The Linux Filesystem Tree
How Linux organises every file in one tree starting at /, what each top-level folder is for, absolute and relative paths, moving around with cd, and reading ls output like a pro, including file types you'll meet on every server.
One tree, starting at /
On Windows, every disk gets a letter: C:\, D:\. Linux works differently: there is one single tree of folders, and everything hangs from the very top, called the root directory and written as a single slash: /.
Extra disks, USB sticks and network shares don't get letters. They are mounted: attached to a folder somewhere inside the tree. A second disk might appear as /data, a network share as /mnt/backups. To you it just looks like more folders. (Mounting gets its own lesson in the storage module.)
The Linux directory tree
Directory is the Linux word for folder. A path is the address of a file or directory in the tree, with / between levels. /etc/nginx/nginx.conf reads: start at /, go into etc, then nginx, and there's the file nginx.conf.
"Root" means two unrelated things. The root directory / is the top of the tree. The root user is the administrator, whose home folder is /root. Context tells you which one people mean.
What each top-level folder is for
Linux distributions follow the Filesystem Hierarchy Standard (FHS), so the same folders exist with the same purpose on Ubuntu, Red Hat, Amazon Linux and almost everything else. This is what lets you find your way on a server you've never seen.
ls -l /What it does: lists the root directory in long format, showing every top-level folder.
total 60
lrwxrwxrwx 1 root root 7 Apr 22 2024 bin -> usr/bin
drwxr-xr-x 2 root root 4096 Apr 22 2024 boot
drwxr-xr-x 5 root root 320 Nov 24 15:15 dev
drwxr-xr-x 1 root root 4096 Nov 24 15:15 etc
drwxr-xr-x 2 root root 4096 Apr 22 2024 home
lrwxrwxrwx 1 root root 7 Apr 22 2024 lib -> usr/lib
drwxr-xr-x 2 root root 4096 Nov 24 07:06 media
drwxr-xr-x 2 root root 4096 Nov 24 07:06 mnt
...Most lines start with d: directories. A few start with l and show an arrow, like bin -> usr/bin: those are links (shortcuts). On modern systems /bin, /sbin and /lib just point into /usr, where the real files live.
Here is what each one holds, roughly in order of how often you'll visit it:
| Directory | What lives there | You'll go there to... |
|---|---|---|
/etc | configuration files, nearly all plain text | change settings: /etc/nginx/, /etc/ssh/sshd_config, /etc/hosts, /etc/passwd |
/var | variable data that grows while the system runs | read logs in /var/log, find app data in /var/lib (databases, Docker), websites in /var/www |
/home | one folder per normal user: /home/aditya | find users' files and their settings |
/root | the root user's home folder | (it's separate so root can log in even if /home is on a broken disk) |
/usr | installed software: programs in /usr/bin, libraries in /usr/lib, docs in /usr/share | find where a program is installed |
/opt | optional software installed by hand, outside the package manager | look for vendor software, like /opt/splunk |
/tmp | temporary files, anyone can write here, often wiped at reboot | store throwaway files |
/srv | data that this server serves to others | shared folders, sometimes web or FTP content |
/boot | the kernel and boot loader files | (rarely; mainly when a server won't boot) |
/dev | devices: disks (/dev/sda, /dev/nvme0n1), terminals, /dev/null | find disk names when adding storage |
/proc, /sys | not on disk: live information from the kernel | read CPU, memory and process details |
/run | runtime data since the last boot: PID files, sockets | (programs use it) |
/mnt, /media | places to mount extra disks and USB drives by hand | attach a disk temporarily |
A closer look at /var
/var is where servers fill up, so it's worth knowing well:
ls /varbackups cache lib local lock log mail opt run spool tmp| Subfolder | Holds |
|---|---|
log | log files: syslog, auth.log, plus one folder per service like nginx/ |
lib | application state: databases (/var/lib/mysql), Docker images (/var/lib/docker), package info |
cache | data that can be rebuilt, for example downloaded packages in /var/cache/apt |
spool | queues of work waiting: mail, print jobs, cron |
tmp | temporary files that should survive a reboot |
When an alert says "disk 95% full", the culprit is very often in /var/log, /var/lib/docker or /var/cache.
/proc: files that aren't really files
Remember "everything is a file"? /proc takes that literally. Its files don't exist on any disk. When you read one, the kernel generates the content on the spot:
grep -m1 "model name" /proc/cpuinfo
grep -c processor /proc/cpuinfo
head -3 /proc/meminfo| Command | What it does |
|---|---|
grep -m1 "model name" /proc/cpuinfo | shows the first line mentioning the CPU model |
grep -c processor /proc/cpuinfo | counts the lines with "processor": one per CPU core |
head -3 /proc/meminfo | the first 3 lines of the memory information |
model name : Intel(R) Xeon(R) Processor @ 2.10GHz
2
MemTotal: 8216208 kB
MemFree: 7000076 kB
MemAvailable: 7648732 kBAn Intel Xeon CPU, 2 cores, about 8 GB of memory (8,216,208 kB), most of it available. Tools like top and free get their numbers from exactly these files.
The tree starts at /
Find the shop app's configuration file and save the port it uses.
Linux distributions follow a standard layout called the Filesystem Hierarchy Standard (FHS). It means config files, logs and programs live in the same places on Ubuntu, Red Hat or Amazon Linux. Once you know the main folders, you can find your way on any server you've never seen before: "where's the config?" → /etc, "where are the logs?" → /var/log.
1. Look at the top of the tree
ls /| Part | Meaning |
|---|---|
ls | list the contents of a directory |
/ | the root directory, the very top of the tree |
bin boot etc lib lib64 mnt proc run sbin.usr-is-merged sys usr
bin.usr-is-merged dev home lib.usr-is-merged media opt root sbin srv tmp varThese are the top-level directories. (The ones ending in .usr-is-merged are leftovers from an Ubuntu upgrade step. Ignore them.)
2. The same, as a tree
tree -L 1 -d /| Part | Meaning |
|---|---|
tree | draw a directory and everything inside it as a tree |
-L 1 | go only 1 Level deep (otherwise it would print the whole system) |
-d | show directories only, no files |
/ | start at the root directory |
/
├── bin -> usr/bin
├── boot
├── dev
├── etc
├── home
├── lib -> usr/lib
...
├── usr
└── var
23 directoriesbin -> usr/bin means bin is a link (a shortcut) that points to usr/bin. On modern systems the real programs live under /usr.
3. What each folder is for
These are the ones you'll use every week as a DevOps engineer:
| Directory | What lives there | Example |
|---|---|---|
/etc | configuration files for the system and for services | /etc/nginx/nginx.conf, /etc/ssh/sshd_config |
/var | variable data that grows and changes | /var/log (logs), /var/www (websites), /var/lib (databases, Docker) |
/home | personal folders of normal users | /home/aditya |
/root | the home folder of the root user | where you are now |
/usr | installed programs and their files | /usr/bin/ls, /usr/bin/python3 |
/opt | optional software installed by hand, outside the package manager | /opt/shopapp |
/tmp | temporary files, often emptied at reboot | anything short-lived |
/srv | data a server serves to others | shared folders, FTP |
/proc, /sys | not real files: a live view of the kernel, processes and hardware | /proc/cpuinfo |
/dev | devices: disks, terminals | /dev/sda |
/boot | the kernel and boot loader | |
/mnt, /media | where extra disks and USB sticks get attached |
4. Apply it: find the shop app's files
The shop app is installed on this machine. Using the table, you can guess where everything is without searching:
ls /etc/shopapp
ls /var/log/shopapp
ls /opt/shopapp/binWhat it does: lists the app's config folder, its log folder and its program folder. You can give ls a path anywhere in the tree, you don't need to go there first.
shopapp.conf
app.log
shopappConfig in /etc, logs in /var/log, the program in /opt: exactly where the standard says.
To read the config file, use cat (it prints a file's contents):
cat /etc/shopapp/shopapp.confFind out which port the shop app is configured to use, and save just the number into /root/shop-port.txt.
Expected result: cat /root/shop-port.txt prints the port number from the config file.
Hint: the commands, explained
The config file says port=.... Then for example:
echo 1234 > /root/shop-port.txtWhat each part does
- You found the port number by reading files with
catorlesswhile moving around withcd.echo 1234 > FILEwrites the number you found into the answer file. Use the number you actually found.
Absolute and relative paths
There are two ways to write a path, like two ways to give directions:
| Absolute path | Relative path | |
|---|---|---|
| Starts with | / | anything else |
| Read from | the top of the tree | where you are now |
| Works from | anywhere | only from the right starting point |
| Example | /var/log/nginx/error.log | nginx/error.log (if you're in /var/log) |
| Like | a full postal address | "second door on the left" |
Three special names make relative paths powerful. They work in every directory:
| Name | Means | Example from /var/log/nginx |
|---|---|---|
. | this directory | ./error.log = /var/log/nginx/error.log |
.. | the parent: one level up | .. = /var/log |
~ | your home directory | ~ = /root for root, /home/aditya for aditya |
Reading the relative path ../../www/shop from /var/log/nginx
/var/log/nginx/var/log/var/var/www/var/www/shopWhen to use which: absolute paths in scripts, cron jobs and config files, because they work no matter where the script runs from. Relative paths when moving around by hand, or inside a project.
Moving around: pwd and cd
Your shell is always "standing" in one directory, the current or working directory. Commands work there unless you give them another path.
pwd
cd /var/log
pwd| Command | What it does |
|---|---|
pwd | prints the current directory |
cd /var/log | change directory to /var/log. Prints nothing when it works. |
/root
/var/logYour prompt changes too: root@devopsshack:/var/log#.
All the ways to use cd:
| Command | Takes you to |
|---|---|
cd /etc/nginx | an absolute path |
cd nginx | the folder nginx inside the current one |
cd .. | one level up |
cd ../.. | two levels up |
cd or cd ~ | your home directory |
cd ~/projects | the projects folder in your home |
cd - | back to the previous directory (and prints it). Run again to switch back. |
When cd fails, you stay where you were:
$ cd /var/lg
bash: cd: /var/lg: No such file or directory
$ cd /etc/hosts
bash: cd: /etc/hosts: Not a directoryNo such file or directory is a typo or a missing folder. Not a directory means the path is a file, and you can only cd into directories. Use Tab completion and the first error disappears.
Moving with cd and absolute paths
Go to the shop app's log folder.
Your shell is always "standing" in one directory, called the current directory or working directory. Commands work there unless you give them another path. Two commands handle this:
pwdtells you where you are.cd(change directory) moves you somewhere else.
An absolute path starts with /. It is the full address from the top of the tree, so it works no matter where you are standing, like a full postal address.
1. Where am I?
pwd/rootYou start in your home folder. The prompt shows the same thing as ~.
2. Move with an absolute path
cd /etc
pwd| Part | Meaning |
|---|---|
cd | change the current directory |
/etc | where to go. It starts with /, so it's an absolute path. |
/etccd itself prints nothing when it works. Your prompt changes to root@devopsshack:/etc#, and pwd confirms it.
3. Go deeper in one jump
You don't need to go one folder at a time. Give the full path:
cd /var/log/shopapp
pwd
ls -lWhat it does: jumps straight into /var/log/shopapp, prints where you are, then lists what's there. ls without a path lists the current directory.
/var/log/shopapp
total 4
-rw-r--r-- 1 root root 85 Nov 24 14:58 app.logNow that you're here, you can use short names: cat app.log instead of cat /var/log/shopapp/app.log.
4. When cd says no
cd /var/log/shopap
cd /etc/shopapp/shopapp.confbash: cd: /var/log/shopap: No such file or directory
bash: cd: /etc/shopapp/shopapp.conf: Not a directory- No such file or directory: the path doesn't exist. Usually a typo, here a missing
p. Tab completion prevents this. - Not a directory: the path exists, but it's a file. You can only
cdinto directories. To read a file, usecat.
When cd fails, you stay where you were.
Go to the shop app's log directory, /var/log/shopapp, and stay there.
Expected result: pwd prints /var/log/shopapp, and your prompt shows the same path.
Hint: the commands, explained
cd /var/log/shopappWhat each part does
cd /var/log/shopapp: change your current folder to this one, using an absolute path (it starts with/, so it works from anywhere).- Run
pwdafterwards: it prints the folder you're in now.
Relative paths
Follow the clues through the company folders and save the final code.
A relative path does not start with /. It is read starting from where you are now, like directions: "go up one floor, then second door on the left". Relative paths are shorter, and they're how most scripts and project files refer to each other.
Two special names make relative paths work. They exist inside every directory:
| Name | Means | Example from /var/log/shopapp |
|---|---|---|
. | this directory | ./app.log is /var/log/shopapp/app.log |
.. | the directory one level up (the parent) | .. is /var/log |
You're in /var/log/shopapp from the last step. Try these one at a time and run pwd after each.
1. Up one level
cd ..
pwdWhat it does: moves to the parent directory.
/var/logFrom /var/log/shopapp, one level up is /var/log.
2. Up two levels, then down again
cd /var/log/shopapp
cd ../../www/shop
pwdRead the relative path ../../www/shop from left to right, starting in /var/log/shopapp:
| Piece | You are now in |
|---|---|
| (start) | /var/log/shopapp |
.. | /var/log |
.. | /var |
www | /var/www |
shop | /var/www/shop |
/var/www/shop3. Absolute or relative: same place, different directions
Both of these take you to the same directory:
cd /var/www/shop # absolute: works from anywhere
cd ../../www/shop # relative: only works from /var/log/shopappUse absolute paths in scripts that might run from anywhere (cron jobs, services), and relative paths when moving around by hand or inside a project.
4. The treasure hunt
Your colleague left a code in the company's shared folder, with clues that only use relative directions. Start here:
cd /srv/company
ls
cat README.txtREADME.txt engineering hr sales
Clue 1: go into the "engineering" folder, then into "platform", and read clue.txt there.Follow each clue with cd and a relative path (no leading /), then read the next clue.txt with cat. Use ls and pwd whenever you're unsure where you are.
Follow the clues from /srv/company and save the final code into /root/code.txt.
Expected result: cat /root/code.txt prints the code from the last folder.
Hint: the commands, explained
cd /srv/company
cd engineering/platform && cat clue.txt
cd ../../sales/reports && cat clue.txt
cd ../../hr/policies && cat code.txt
cat code.txt > /root/code.txtWhat each part does
cd engineering/platform: a relative path, starting from where you are now.&& cat clue.txtreads the clue only if the cd worked.cd ../../sales/reports:..means "the parent folder". Two of them go up two levels (back to/srv/company), then down intosales/reports.cat code.txt > /root/code.txt: print the file, but send the output into a new file instead of the screen.
Listing in depth: ls
You met ls in the last lesson. It's worth mastering, because you'll run it more than any other command.
The long format, column by column
ls -l /etc-rw-r--r-- 1 root root 2319 Mar 31 2024 bash.bashrc
drwxr-xr-x 2 root root 4096 Nov 24 09:14 cron.d
-rw-r--r-- 1 root root 20 Nov 24 15:15 hosts
lrwxrwxrwx 1 root root 27 Nov 24 09:14 localtime -> /usr/share/zoneinfo/Etc/UTC| Column | Example | Meaning |
|---|---|---|
| 1 | -rw-r--r-- | type (first character) and permissions (the other nine) |
| 2 | 1 | number of hard links (explained in the links lesson) |
| 3 | root | the owner |
| 4 | root | the group |
| 5 | 2319 | the size in bytes |
| 6 | Mar 31 2024 | last modified. Within the last 6 months it shows the time instead of the year. |
| 7 | bash.bashrc | the name. For links, -> shows where it points. |
File types: the first character
| Character | Type | Example |
|---|---|---|
- | regular file: text, programs, images, anything | /etc/hosts |
d | directory | /etc/cron.d |
l | symbolic link (a shortcut to another path) | /bin -> usr/bin |
c | character device: a device read byte by byte | /dev/null, /dev/tty |
b | block device: a disk | /dev/sda, /dev/nvme0n1 |
s | socket: for programs to talk to each other | /run/docker.sock |
p | named pipe | (rare) |
ls -l /dev/null /bincrw-rw-rw- 1 root root 1, 3 Nov 24 15:15 /dev/null
lrwxrwxrwx 1 root root 7 Apr 22 2024 /bin -> usr/bin/dev/null is a character device. Anything written to it disappears: it's the system's black hole, used to throw away unwanted output (command > /dev/null). For devices, the size column shows two device numbers (1, 3) instead of a size.
The ls options worth knowing
| Option | Effect |
|---|---|
-l | long format |
-a | all, including hidden files (names starting with .) |
-A | like -a, but without . and .. |
-h | human-readable sizes (136K, 2.1G), together with -l |
-t | sort by modification time, newest first |
-r | reverse the sort order |
-S | sort by size, largest first |
-R | recursive: list subfolders too |
-d | list the directory itself, not its contents |
-1 | one name per line (handy in scripts) |
-i | show inode numbers (links lesson) |
They combine into everyday commands:
ls -lhS /var/log
ls -ltr /etc
ls -ld /tmp /etc /root| Command | Answers the question |
|---|---|
ls -lhS /var/log | "Which log files are the biggest?" |
ls -ltr /etc | "What changed most recently?" Reverse time order puts the newest at the bottom, right above your prompt. |
ls -ld /tmp /etc /root | "What are the permissions of these folders themselves?" |
total 224K
-rw-r--r-- 1 root root 136K Nov 24 10:05 dpkg.log
-rw-r--r-- 1 root root 61K Nov 24 07:06 bootstrap.log
-rw-r--r-- 1 root root 9.8K Nov 24 09:14 alternatives.log-rw-r--r-- 1 root root 12 Nov 24 15:15 hostname
-rw-r--r-- 1 root root 265 Nov 24 15:15 resolv.conf
-rw-r--r-- 1 root root 20 Nov 24 15:15 hostsdrwxr-xr-x 1 root root 4096 Nov 24 15:15 /etc
drwx------ 1 root root 4096 Nov 24 09:14 /root
drwxrwxrwt 1 root root 4096 Nov 24 15:15 /tmp- The biggest log here is
dpkg.log(136 KB), the package manager's history. - The most recently changed files in
/etcarehostname,resolv.confandhosts, written when this machine started. /rootisdrwx------: only root can enter it./tmpends int(rwxrwxrwt): everyone can write there, but only delete their own files. You'll decode these letters in the permissions lesson.
Hidden files
Any name starting with a dot is hidden from plain ls. It's not a security feature, just a way to keep settings out of the way:
ls -la /roottotal 20
drwx------ 1 root root 4096 Nov 24 09:14 .
drwxr-xr-x 1 root root 4096 Nov 24 15:15 ..
-rw-r--r-- 1 root root 3106 Apr 22 2024 .bashrc
-rw-r--r-- 1 root root 161 Apr 22 2024 .profile
drwx------ 2 root root 4096 Nov 24 09:14 .sshRoot's home folder looks empty with ls, but it holds .bashrc and .profile (shell settings) and .ssh (SSH keys). Configuration files and folders like these are called dotfiles.
Seeing a whole tree
tree /etc/ssh # draw the folder and everything below it
tree -L 2 -d /var # only 2 levels deep, directories only
ls -R /etc/ssh # the same idea, when tree isn't installedShortcuts and overviews
Return to your home folder and save a map of the company folders.
A few shortcuts save a lot of typing, and two commands give you an overview of a whole folder at once, so you don't have to cd into every subfolder to see what's there.
1. Home, sweet home: ~ and plain cd
| Shortcut | Means |
|---|---|
~ | your home directory (/root for root, /home/NAME for others) |
cd (with nothing after it) | go to your home directory |
cd ~ | the same |
~/notes.txt | the file notes.txt in your home directory, from anywhere |
cd /etc/shopapp
cd
pwd/rootHowever lost you are, plain cd brings you home.
2. Back and forth: cd -
cd /var/log/shopapp
cd /etc/shopapp
cd -What cd - does: goes back to the previous directory you were in, and prints its path. Run it again to switch back. Very handy when you're comparing a config file and a log file in two different places.
/var/log/shopapp3. See a whole tree: tree
tree /srv/companyWhat it does: draws the directory and everything below it.
/srv/company
├── README.txt
├── engineering
│ ├── backend
│ └── platform
│ ├── clue.txt
│ └── runbooks
│ └── deploy.md
├── hr
│ └── policies
│ ├── code.txt
│ └── leave.md
└── sales
└── reports
├── clue.txt
└── q3.txt
9 directories, 7 files- Each level of indentation is one level deeper in the tree.
├──means "there's more below",└──means "the last item in this folder".- The summary line counts everything. On big trees, limit the depth with
-L 2.
4. When tree isn't installed: ls -R
tree isn't installed on every server. ls -R (Recursive) is always there:
ls -R /srv/company/engineering/srv/company/engineering:
backend
platform
/srv/company/engineering/backend:
/srv/company/engineering/platform:
clue.txt
runbooks
/srv/company/engineering/platform/runbooks:
deploy.mdEach block starts with a directory's path and a colon, then lists its contents. backend is empty, so its block has nothing in it.
- Go back to your home directory.
- Save a tree view of
/srv/companyinto/root/company-tree.txt.
Expected result: pwd prints /root, and the file contains the tree with 9 directories, 7 files at the end.
Hint: the commands, explained
cd
tree /srv/company > /root/company-tree.txtWhat each part does
cdwith nothing after it takes you to your home folder (/rootfor root).tree /srv/company: draw the folder and everything inside it as a tree.>saves that drawing into a file.
Wildcards for paths
You'll learn wildcards properly in the next lesson, but they're handy for exploring right away. * means "any characters":
ls -d /etc/s* # everything in /etc whose name starts with s
ls /var/log/*.log # every .log file directly in /var/logMixing up / at the start of a path. cd /etc goes to the top-level /etc. cd etc looks for a folder called etc inside the current directory, which usually doesn't exist. If a path should start from the top, it starts with /.
"Where would you look for configuration files? For logs?" Configuration in /etc, logs in /var/log. Application data in /var/lib, temporary files in /tmp.
"What's the difference between an absolute and a relative path?" An absolute path starts with / and works from anywhere; a relative path starts from the current directory. Scripts should use absolute paths.
"What is /proc?" A virtual filesystem the kernel uses to show live information about processes and hardware. Its files take no disk space.
"What is /dev/null?" A special device that discards anything written to it. Used to throw away output, as in cron-job > /dev/null 2>&1.
Summary
- Linux has one tree starting at
/. Disks are mounted into it, not given letters. - Know the key folders:
/etcconfig,/var/loglogs,/var/libapp data,/homeand/roothome folders,/usr/binprograms,/opthand-installed software,/tmptemporary,/devdevices,/proclive kernel info. - Absolute paths start with
/; relative paths start from where you are..here,..up,~home. pwdshows where you are;cdmoves (cdalone goes home,cd -goes back).ls -lcolumns: type + permissions, links, owner, group, size, date, name. The first character is the type:-file,ddirectory,llink,c/bdevices.- Everyday combinations:
ls -lah,ls -lhS(biggest first),ls -ltr(newest last),ls -ld DIR.