The shell and the filesystem
Read a prompt, find your way around the Linux directory tree, and create, copy, move and delete files without breaking anything.
By the end of this lesson you'll be able to
0 of 4 completeWhat the shell actually is
Every Linux server you'll ever look after is run from a terminal. There's no desktop and no mouse, just a shell: a program that reads the command you type, runs it, and prints what it says back. On most servers that shell is bash; on a Mac it's zsh. For everything in this lesson they behave the same.
Before you type anything, read the prompt. It tells you who you are, where you are and how careful you need to be:
rajeev@web-01:~/app$
| Part | Means |
|---|---|
rajeev | The user you're logged in as |
web-01 | The machine's hostname. Check it before you delete anything on a server |
~/app | The directory you're in. ~ is short for your home directory |
$ / # | $ for a normal user, # for root, who can change anything on the box |
A command is a program name, then options (usually starting with -), then arguments:
$ ls -l /etc/hostname # program: ls · option: -l (long format) · argument: /etc/hostname -rw-r--r-- 1 root root 7 Mar 2 09:14 /etc/hostname $ whoami # who am I logged in as? rajeev $ ls --help | head -n 5 # almost every command explains itself with --help
ls -l /etc/hostnamelslist files-llong format: permissions, owner, size, date
whoamiwhoamiprint your user name
ls --help | head -n 5|pipe: send output to the next commandheadshow the first lines-nhow many lines
How the filesystem is laid out
Linux has one tree, starting at / (called "root"). There are no C: or D: drives:
extra disks are attached as folders somewhere inside the tree. Every server you log in to uses
roughly the same layout, so once you learn it you know where to look on any of them.
| Path | What lives there | You go there to |
|---|---|---|
/etc | System-wide configuration: nginx/, ssh/sshd_config, hosts | Change how a service behaves |
/var/log | Log files, written by services as they run | Find out what went wrong |
/home/<user> | Each user's own files. Root's home is /root | Keep your scripts and notes |
/usr/bin, /usr/local/bin | Programs. Packages go in /usr/bin, your own installs in local | See where a command comes from |
/opt | Software installed outside the package manager | Find a vendor's app |
/tmp | Temporary files, often cleared on reboot | Scratch work you can lose |
/proc | Not real files: live information from the kernel | Read CPU, memory and process facts |
Try cat /proc/cpuinfo or cat /proc/meminfo. Nothing is stored on disk there; the kernel
writes the answer the moment you read it.
Moving around
$ pwd # print working directory: where am I? /home/rajeev $ ls # what's here? $ ls -la # long format, including hidden files (names starting with .) $ cd /var/log # go to an absolute path $ cd .. # up one level, to /var $ cd - # back to the previous directory, /var/log $ cd # no argument: straight home
pwdpwdprint the directory you are in
lslslist files
ls -la-llong format: permissions, owner, size, date-ainclude hidden files (names starting with .)
cd /var/logcdchange directory
cd ....the folder above this one
cd --the previous directory
There are two ways to write a path, and you'll use both every day:
- An absolute path starts with
/and works from anywhere:/var/log/nginx/error.log. - A relative path starts from where you are now.
.is the current directory and..is the one above it. From/var/www,../logmeans/var/log.
Use absolute paths in scripts, cron jobs and config files, because they don't depend on where the command happens to run. Use relative paths when you're moving around by hand.
Create, copy, move and delete
$ mkdir -p ~/projects/demo/logs # -p creates every missing parent folder, no error if it exists $ cd ~/projects/demo $ touch notes.txt # create an empty file (or update an existing file's time) $ cp notes.txt notes.bak # copy a file $ cp -r logs logs-old # copy a folder and everything in it: -r means recursive $ mv notes.bak archive.txt # rename... $ mv archive.txt logs/ # ...or move. mv does both $ rm archive.txt # fails: it's in logs/ now rm: cannot remove 'archive.txt': No such file or directory $ rm logs/archive.txt # delete a file $ rmdir logs-old # delete a folder, only if it's empty $ rm -r logs-old # delete a folder and everything in it
mkdir -p ~/projects/demo/logsmkdirmake a directory-pcreate missing parents, no error if it exists
cd ~/projects/democdchange directory
touch notes.txttouchcreate an empty file, or update its time
cp notes.txt notes.bakcpcopy files
cp -r logs logs-old-rcopy folders and everything inside
mv notes.bak archive.txtmvmove or rename
rm archive.txtrmdelete files, with no undo
rmdir logs-oldrmdirremove an empty directory
rm -r logs-old-rdelete folders and everything inside
Read files without opening an editor
| Command | Use it to |
|---|---|
cat file | Print a short file in one go |
less file | Scroll through a long file. /word searches, q quits |
head -n 20 file | See the first 20 lines |
tail -n 50 file | See the last 50 lines, where the newest log entries are |
tail -f file | Keep following a log as new lines arrive. Ctrl+C stops it |
Find files
$ find /etc -name "*.conf" # every .conf file under /etc $ find ~/projects -mtime -1 # files changed in the last 24 hours $ find /var/log -name "*.log" -size +10M # logs bigger than 10 MB
find /etc -name "*.conf"findsearch for files-namematch the file name (wildcards allowed)
find ~/projects -mtime -1-mtimeby days since last change (-1 = last 24 hours)
find /var/log -name "*.log" -size +10M-sizeby size, like +10M
Lay out a release folder
You're shipping version 1.4 of an app. Create ~/deploy/releases/1.3, ~/deploy/releases/1.4 and ~/deploy/shared/logs in one command. Copy /etc/hostname and /etc/os-release into the 1.4 release as a record of the machine. Then check what's inside the old 1.3 release and remove it safely.
~/deploy/releases/1.4/Reveal one safe solution
$ mkdir -p ~/deploy/releases/{1.3,1.4} ~/deploy/shared/logs # {1.3,1.4} expands to both names $ cp /etc/hostname /etc/os-release ~/deploy/releases/1.4/ $ find ~/deploy -type f # every file, so you know what's there /home/rajeev/deploy/releases/1.4/hostname /home/rajeev/deploy/releases/1.4/os-release $ ls -la ~/deploy/releases/1.3 # look before you delete $ rm -r ~/deploy/releases/1.3 $ ls ~/deploy/releases 1.4
mkdir -p ~/deploy/releases/{1.3,1.4} ~/deploy/shared/logsmkdirmake a directory-pcreate missing parents, no error if it exists
cp /etc/hostname /etc/os-release ~/deploy/releases/1.4/cpcopy files
find ~/deploy -type ffindsearch for files-typef for files, d for directories
ls -la ~/deploy/releases/1.3lslist files-llong format: permissions, owner, size, date-ainclude hidden files (names starting with .)
rm -r ~/deploy/releases/1.3rmdelete files, with no undo-rdelete folders and everything inside
cp takes several files and copies them all into the last argument, as long as it's a folder.
Listing 1.3 before deleting it is the habit that saves you on the day the folder isn't empty.
Try it yourself
0 of 6 steps doneRun each task in your own account or terminal, then tick it off.
Quick check
You restart a service and want to watch its log file as new lines arrive. Which command do you run?
Official reading for this lesson
- GuideCommand line for beginnersUbuntu
- DocsFilesystem Hierarchy Standard 3.0Linux Foundation
- Docshier(7): the filesystem hierarchy, in the manualUbuntu manpages
- DocsGNU Coreutils (ls, cp, mv, rm and friends)GNU
- Toolexplainshell, paste a command and see every part explainedexplainshell
- Tooltldr pages, short practical examples for every commandtldr-pages
Or get every quiz answer right and it completes itself.