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/etc/hostnamea path
whoamiwhoamiprint your user name
ls --help | head -n 5lslist files--helpan option|pipe: send output to the next commandheadshow the first lines-nhow many lines5a value the command works on
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 -lalslist files-llong format: permissions, owner, size, date-ainclude hidden files (names starting with .)
cd /var/logcdchange directory/var/loga path
cd ..cdchange directory..the folder above this one
cd -cdchange directory-the previous directory
cdcdchange 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~/projects/demo/logsa path
cd ~/projects/democdchange directory~/projects/demoa path
touch notes.txttouchcreate an empty file, or update its timenotes.txta value the command works on
cp notes.txt notes.bakcpcopy filesnotes.txta value the command works onnotes.baka value the command works on
cp -r logs logs-oldcpcopy files-rcopy folders and everything insidelogsa value the command works onlogs-olda value the command works on
mv notes.bak archive.txtmvmove or renamenotes.baka value the command works onarchive.txta value the command works on
mv archive.txt logs/mvmove or renamearchive.txta value the command works onlogs/a value the command works on
rm archive.txtrmdelete files, with no undoarchive.txta value the command works on
rm logs/archive.txtrmdelete files, with no undologs/archive.txta value the command works on
rmdir logs-oldrmdirremove an empty directorylogs-olda value the command works on
rm -r logs-oldrmdelete files, with no undo-rdelete folders and everything insidelogs-olda value the command works on
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/etca path-namematch the file name (wildcards allowed)"*.conf"text, kept together by the quotes
find ~/projects -mtime -1findsearch for files~/projectsa path-mtimeby days since last change (-1 = last 24 hours)-1a value the command works on
find /var/log -name "*.log" -size +10Mfindsearch for files/var/loga path-namematch the file name (wildcards allowed)"*.log"text, kept together by the quotes-sizeby size, like +10M+10Ma value the command works on
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~/deploy/releases/{1.3,1.4}a path~/deploy/shared/logsa path
cp /etc/hostname /etc/os-release ~/deploy/releases/1.4/cpcopy files/etc/hostnamea path/etc/os-releasea path~/deploy/releases/1.4/a path
find ~/deploy -type ffindsearch for files~/deploya path-typef for files, d for directoriesfa value the command works on
ls -la ~/deploy/releases/1.3lslist files-llong format: permissions, owner, size, date-ainclude hidden files (names starting with .)~/deploy/releases/1.3a path
rm -r ~/deploy/releases/1.3rmdelete files, with no undo-rdelete folders and everything inside~/deploy/releases/1.3a path
ls ~/deploy/releaseslslist files~/deploy/releasesa path
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.