Lesson 2 of 12 · 9 min · Working with files
The filesystem: layout, paths and everyday file commands
What you will learn
- The single directory tree, and what lives in
/etc,/var,/home,/usr,/tmp,/procand others - Absolute and relative paths,
.,..and~ cd,ls,mkdir,cp,mv,rm,touch,catandless- How to delete without regret
In simple words
Think of a Linux system as one big cupboard with labelled shelves. Everything, whether a document, a program or a setting, sits on some shelf, and its address tells you exactly where. This lesson shows you the main shelves and the handful of commands for moving around and for creating, copying and deleting files.
Words to know
- Directory: a folder. It holds files and other folders.
- Path: the address of a file, such as
/home/asha/notes.txt. - Absolute path: the full address, starting from the top (
/). It works from anywhere. - Relative path: an address that starts from the folder you are in now.
One tree, not drive letters
Windows has C: and D:. Linux has one tree that starts at / (called "root", not to be confused with the root user). Extra disks are mounted at a directory inside the tree, for example a data disk at /data. Programs do not care which disk a path is on.
The layout follows a convention (the Filesystem Hierarchy Standard). These are the directories you will use every week:
| Path | What lives there |
|---|---|
/etc |
System configuration files, as text. Where you edit nginx, ssh, cron settings |
/var |
Data that changes while the system runs: logs in /var/log, caches, spool, databases in /var/lib |
/home |
One directory per user (/home/asha). /root is the root user's own home |
/usr |
Installed programs and libraries. On modern distributions /bin, /sbin and /lib are links to /usr/bin, /usr/sbin, /usr/lib |
/opt |
Optional third-party software, installed as one folder each |
/tmp |
Temporary files that any user can create. Often cleared on reboot |
/proc, /sys |
Not real files: the kernel's live view of processes and devices. You read them like files |
/dev |
Device files such as disks (/dev/sda) and special ones like /dev/null (a bin that swallows everything) |
/run |
Runtime state since boot: process ids, sockets |
/boot |
The kernel and boot loader files |
"Everything is a file" is the core idea: a text document, a disk, a terminal and a running process's details are all reached by path with the same tools (cat, ls, redirection). For example cat /proc/cpuinfo shows your CPU, and cat /etc/os-release names the distribution.
Paths
A path says where something is. There are two kinds:
- Absolute: starts with
/and works from anywhere:/var/log/syslog. - Relative: does not start with
/and is read from your current directory:logs/app.log.
Special names: . is the current directory, .. is its parent, ~ is your home directory.
mkdir -p /tmp/demo/projects/app # -p creates missing parents, and is not an error if it exists
cd /tmp/demo
pwd
touch projects/app/main.py projects/notes.txt
/tmp/demo
tree (install it with apt-get install -y tree) draws the structure:
.
`-- projects
|-- app
| `-- main.py
`-- notes.txt
3 directories, 2 files
cd with no argument goes home, and cd - returns to the previous directory.
Listing
ls # names only
ls -l # long: permissions, owner, group, size, date, name
ls -la # include hidden (dot) files
ls -lh # sizes in human units: 4.0K, 12M
ls -lt # newest first
ls -ld /tmp # about the directory itself, not its contents
Create, copy, move, delete
cp notes.txt notes.bak # copy a file
cp -r projects projects-backup # -r copies a directory and everything in it
mv notes.bak old-notes.txt # move or rename (same command)
mv old-notes.txt projects/ # move into a directory
rm projects/old-notes.txt # delete a file
rmdir emptydir # delete an empty directory
rm -r projects-backup # delete a directory and its contents
Notes that matter:
mvis a rename when source and destination are in the same place, and a move otherwise. Either way it overwrites an existing destination file silently. Add-ito be asked first.- There is no recycle bin.
rmremoves the name straight away. Before runningrm -ron a path you typed, runlson the same path first. rm -rf /some/dirforces deletion without questions. The classic disaster is an unset variable makingrm -rf "$DIR/"becomerm -rf /. Quote variables, and preferrm -rwithout-f, so a typo asks questions.
Looking inside files
cat a.txt # print the whole file
less /var/log/syslog # page through a long file: space = next page, / = search, q = quit
head -n 5 a.txt # first 5 lines
tail -n 5 a.txt # last 5 lines
tail -f app.log # keep following new lines as they are written (Ctrl+C to stop)
wc -l a.txt # count lines
tail -f is the most used command during incidents, because it shows a log as it happens.
Wildcards (globbing)
The shell expands patterns before the command runs:
*matches any characters:ls *.log?matches one character:ls file?.txt[abc]matches one of a set:ls report-[123].csv{a,b}expands to alternatives:mkdir -p site/{css,js,img}
Because the shell does the expanding, rm *.log runs rm with a list of actual file names. If nothing matches, bash passes the pattern itself unchanged, which is why you sometimes see errors quoting *.log.
Hard links and symbolic links
A symbolic link (symlink) is a small file that points to another path, like a shortcut: ln -s /opt/app-1.4 /opt/app makes /opt/app lead to version 1.4. Deployments use this trick: switch the link to a new version directory and the "current" path changes at once. A hard link (ln without -s) is a second name for the same data on the same disk; deleting one name does not delete the data while another name exists. Use symlinks unless you know you need hard links.
Common mistakes
- Spaces in names.
rm my filetries to delete two things,myandfile. Quote:rm "my file". - Relative paths from the wrong place. Run
pwdwhen a command says "No such file or directory" for something you are sure exists. - Editing in
/tmpand expecting it to stay./tmpis for temporary data. - Treating
/procfiles as storage. They are generated by the kernel when read.
Try it yourself
- Create
~/lab/{logs,config,scripts}in one command. Put an emptyapp.loginlogs. Show the structure withls -R ~/lab. - Copy
~/lab/logsto~/lab/logs-oldand then delete the copy, checking withlsbefore and after. - Use
ln -sto create~/lab/currentpointing to~/lab/config, thenls -l ~/labto see the arrow. - Open
/etc/os-releasewithless, search forVERSIONwith/, and quit.
Check yourself
What is the difference between /home/asha/app and app?
The first is an absolute path and works from anywhere. The second is relative to the current directory, so it only works where app actually is. ~/app for user asha means the same as the first.
What does mkdir -p a/b/c do that mkdir a/b/c does not?
It creates any missing parent directories (a and a/b) and does not complain if directories already exist. Without -p the command fails when a/b is missing.
Why is rm -rf $DIR/* dangerous in a script, and how do you make it safer?
If DIR is empty or unset it expands to rm -rf /*. Quote and guard it: rm -rf "${DIR:?DIR is not set}"/*, which aborts when DIR is empty, or avoid -f and test the variable first.
Which directory would you look in for an application's logs, and which for its configuration?
Logs under /var/log (or the application's directory under it), configuration under /etc.
Interview questions
What is the difference between a hard link and a symbolic link?
A hard link is another name for the same inode (the same data) and cannot cross filesystems or point to directories. A symlink is a separate file holding a path; it can cross filesystems and point to directories, and it breaks if the target is removed.
What are /proc and /dev?
/proc is a virtual filesystem where the kernel exposes process and system information as files. /dev holds device files that give access to hardware and pseudo-devices such as /dev/null and /dev/zero.
How do you see what is filling up a directory? (Preview of the troubleshooting lessons)
du -sh * inside the directory shows the size of each entry; sort with du -sh * | sort -h.
Summary
- Linux has one tree starting at
/; know/etc(config),/var/log(logs),/home,/usr,/tmpand the virtual/procand/dev. - Absolute paths start with
/; relative ones start where you are..,..and~are shortcuts. cp -r,mv,rm -r,mkdir -p,ln -s,lessandtail -fcover most daily work.- Nothing is recoverable after
rm. List first, quote variables, avoid-funless needed.
Going deeper
- Filesystem Hierarchy Standard (Linux Foundation): the convention behind the directory layout.
- GNU Coreutils manual: every option of
ls,cp,mv,rm,lnand more. - hier(7) manual page: the directory tree as documented on Linux.
See how much of this you can explain
A free mock interview asks you new questions on Linux for DevOps for DevOps Engineer and scores every answer, with what to fix.