Learn / Linux / Getting Started
Lesson 4 of 74

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.

25 min read Report a problem Hands-on lab included 6-question quiz

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

/the root directory: the top of everything
/etcconfiguration
/varlogs, data, websites
/homeusers' folders
/rootroot's home
/usrinstalled programs
/tmptemporary files
/etc/nginx/nginx.confa config file
/var/log/sysloga log file
/home/aditya/notes.txta user's file
/usr/bin/python3a program

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.

bash
ls -l /

What it does: lists the root directory in long format, showing every top-level folder.

text
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:

DirectoryWhat lives thereYou'll go there to...
/etcconfiguration files, nearly all plain textchange settings: /etc/nginx/, /etc/ssh/sshd_config, /etc/hosts, /etc/passwd
/varvariable data that grows while the system runsread logs in /var/log, find app data in /var/lib (databases, Docker), websites in /var/www
/homeone folder per normal user: /home/adityafind users' files and their settings
/rootthe root user's home folder(it's separate so root can log in even if /home is on a broken disk)
/usrinstalled software: programs in /usr/bin, libraries in /usr/lib, docs in /usr/sharefind where a program is installed
/optoptional software installed by hand, outside the package managerlook for vendor software, like /opt/splunk
/tmptemporary files, anyone can write here, often wiped at rebootstore throwaway files
/srvdata that this server serves to othersshared folders, sometimes web or FTP content
/bootthe kernel and boot loader files(rarely; mainly when a server won't boot)
/devdevices: disks (/dev/sda, /dev/nvme0n1), terminals, /dev/nullfind disk names when adding storage
/proc, /sysnot on disk: live information from the kernelread CPU, memory and process details
/runruntime data since the last boot: PID files, sockets(programs use it)
/mnt, /mediaplaces to mount extra disks and USB drives by handattach a disk temporarily

A closer look at /var

/var is where servers fill up, so it's worth knowing well:

bash
ls /var
text
backups  cache  lib  local  lock  log  mail  opt  run  spool  tmp
SubfolderHolds
loglog files: syslog, auth.log, plus one folder per service like nginx/
libapplication state: databases (/var/lib/mysql), Docker images (/var/lib/docker), package info
cachedata that can be rebuilt, for example downloaded packages in /var/cache/apt
spoolqueues of work waiting: mail, print jobs, cron
tmptemporary 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:

bash
grep -m1 "model name" /proc/cpuinfo
grep -c processor /proc/cpuinfo
head -3 /proc/meminfo
CommandWhat it does
grep -m1 "model name" /proc/cpuinfoshows the first line mentioning the CPU model
grep -c processor /proc/cpuinfocounts the lines with "processor": one per CPU core
head -3 /proc/meminfothe first 3 lines of the memory information
text
model name	: Intel(R) Xeon(R) Processor @ 2.10GHz
2
MemTotal:        8216208 kB
MemFree:         7000076 kB
MemAvailable:    7648732 kB

An 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.

Try it · Task 1 of 4

The tree starts at /

Find the shop app's configuration file and save the port it uses.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
ls /
PartMeaning
lslist the contents of a directory
/the root directory, the very top of the tree
text
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  var

These 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

bash
tree -L 1 -d /
PartMeaning
treedraw a directory and everything inside it as a tree
-L 1go only 1 Level deep (otherwise it would print the whole system)
-dshow directories only, no files
/start at the root directory
text
/
├── bin -> usr/bin
├── boot
├── dev
├── etc
├── home
├── lib -> usr/lib
...
├── usr
└── var

23 directories

bin -> 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:

DirectoryWhat lives thereExample
/etcconfiguration files for the system and for services/etc/nginx/nginx.conf, /etc/ssh/sshd_config
/varvariable data that grows and changes/var/log (logs), /var/www (websites), /var/lib (databases, Docker)
/homepersonal folders of normal users/home/aditya
/rootthe home folder of the root userwhere you are now
/usrinstalled programs and their files/usr/bin/ls, /usr/bin/python3
/optoptional software installed by hand, outside the package manager/opt/shopapp
/tmptemporary files, often emptied at rebootanything short-lived
/srvdata a server serves to othersshared folders, FTP
/proc, /sysnot real files: a live view of the kernel, processes and hardware/proc/cpuinfo
/devdevices: disks, terminals/dev/sda
/bootthe kernel and boot loader
/mnt, /mediawhere 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:

bash
ls /etc/shopapp
ls /var/log/shopapp
ls /opt/shopapp/bin

What 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.

text
shopapp.conf
app.log
shopapp

Config 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):

bash
cat /etc/shopapp/shopapp.conf
2 · Your task

Find 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:

text
echo 1234 > /root/shop-port.txt

What each part does

  • You found the port number by reading files with cat or less while moving around with cd. echo 1234 > FILE writes the number you found into the answer file. Use the number you actually found.
First start the lab machine in the terminal panel on the right.

Absolute and relative paths

There are two ways to write a path, like two ways to give directions:

Absolute pathRelative path
Starts with/anything else
Read fromthe top of the treewhere you are now
Works fromanywhereonly from the right starting point
Example/var/log/nginx/error.lognginx/error.log (if you're in /var/log)
Likea full postal address"second door on the left"

Three special names make relative paths powerful. They work in every directory:

NameMeansExample 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

start/var/log/nginx
..
up/var/log
..
up/var
www
down/var/www
shop
arrived/var/www/shop

When 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.

bash
pwd
cd /var/log
pwd
CommandWhat it does
pwdprints the current directory
cd /var/logchange directory to /var/log. Prints nothing when it works.
text
/root
/var/log

Your prompt changes too: root@devopsshack:/var/log#.

All the ways to use cd:

CommandTakes you to
cd /etc/nginxan absolute path
cd nginxthe folder nginx inside the current one
cd ..one level up
cd ../..two levels up
cd or cd ~your home directory
cd ~/projectsthe 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:

text
$ cd /var/lg
bash: cd: /var/lg: No such file or directory

$ cd /etc/hosts
bash: cd: /etc/hosts: Not a directory

No 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.

Try it · Task 2 of 4

Moving with cd and absolute paths

Go to the shop app's log folder.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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:

  • pwd tells 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?

bash
pwd
text
/root

You start in your home folder. The prompt shows the same thing as ~.

2. Move with an absolute path

bash
cd /etc
pwd
PartMeaning
cdchange the current directory
/etcwhere to go. It starts with /, so it's an absolute path.
text
/etc

cd 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:

bash
cd /var/log/shopapp
pwd
ls -l

What 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.

text
/var/log/shopapp
total 4
-rw-r--r-- 1 root root 85 Nov 24 14:58 app.log

Now 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

bash
cd /var/log/shopap
cd /etc/shopapp/shopapp.conf
text
bash: 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 cd into directories. To read a file, use cat.

When cd fails, you stay where you were.

2 · Your task

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
text
cd /var/log/shopapp

What 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 pwd afterwards: it prints the folder you're in now.
First start the lab machine in the terminal panel on the right.
Try it · Task 3 of 4

Relative paths

Follow the clues through the company folders and save the final code.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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:

NameMeansExample 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

bash
cd ..
pwd

What it does: moves to the parent directory.

text
/var/log

From /var/log/shopapp, one level up is /var/log.

2. Up two levels, then down again

bash
cd /var/log/shopapp
cd ../../www/shop
pwd

Read the relative path ../../www/shop from left to right, starting in /var/log/shopapp:

PieceYou are now in
(start)/var/log/shopapp
../var/log
../var
www/var/www
shop/var/www/shop
text
/var/www/shop

3. Absolute or relative: same place, different directions

Both of these take you to the same directory:

text
cd /var/www/shop          # absolute: works from anywhere
cd ../../www/shop         # relative: only works from /var/log/shopapp

Use 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:

bash
cd /srv/company
ls
cat README.txt
text
README.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.

2 · Your task

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
text
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.txt

What each part does

  • cd engineering/platform: a relative path, starting from where you are now. && cat clue.txt reads 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 into sales/reports.
  • cat code.txt > /root/code.txt: print the file, but send the output into a new file instead of the screen.
First start the lab machine in the terminal panel on the right.

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

bash
ls -l /etc
text
-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
ColumnExampleMeaning
1-rw-r--r--type (first character) and permissions (the other nine)
21number of hard links (explained in the links lesson)
3rootthe owner
4rootthe group
52319the size in bytes
6Mar 31 2024last modified. Within the last 6 months it shows the time instead of the year.
7bash.bashrcthe name. For links, -> shows where it points.

File types: the first character

CharacterTypeExample
-regular file: text, programs, images, anything/etc/hosts
ddirectory/etc/cron.d
lsymbolic link (a shortcut to another path)/bin -> usr/bin
ccharacter device: a device read byte by byte/dev/null, /dev/tty
bblock device: a disk/dev/sda, /dev/nvme0n1
ssocket: for programs to talk to each other/run/docker.sock
pnamed pipe(rare)
bash
ls -l /dev/null /bin
text
crw-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

OptionEffect
-llong format
-aall, including hidden files (names starting with .)
-Alike -a, but without . and ..
-hhuman-readable sizes (136K, 2.1G), together with -l
-tsort by modification time, newest first
-rreverse the sort order
-Ssort by size, largest first
-Rrecursive: list subfolders too
-dlist the directory itself, not its contents
-1one name per line (handy in scripts)
-ishow inode numbers (links lesson)

They combine into everyday commands:

bash
ls -lhS /var/log
ls -ltr /etc
ls -ld /tmp /etc /root
CommandAnswers 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?"
text
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
text
-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 hosts
text
drwxr-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 /etc are hostname, resolv.conf and hosts, written when this machine started.
  • /root is drwx------: only root can enter it. /tmp ends in t (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:

bash
ls -la /root
text
total 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 .ssh

Root'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

text
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 installed
Try it · Task 4 of 4

Shortcuts and overviews

Return to your home folder and save a map of the company folders.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

ShortcutMeans
~your home directory (/root for root, /home/NAME for others)
cd (with nothing after it)go to your home directory
cd ~the same
~/notes.txtthe file notes.txt in your home directory, from anywhere
bash
cd /etc/shopapp
cd
pwd
text
/root

However lost you are, plain cd brings you home.

2. Back and forth: cd -

bash
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.

text
/var/log/shopapp

3. See a whole tree: tree

bash
tree /srv/company

What it does: draws the directory and everything below it.

text
/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:

bash
ls -R /srv/company/engineering
text
/srv/company/engineering:
backend
platform

/srv/company/engineering/backend:

/srv/company/engineering/platform:
clue.txt
runbooks

/srv/company/engineering/platform/runbooks:
deploy.md

Each block starts with a directory's path and a colon, then lists its contents. backend is empty, so its block has nothing in it.

2 · Your task
  1. Go back to your home directory.
  2. Save a tree view of /srv/company into /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
text
cd
tree /srv/company > /root/company-tree.txt

What each part does

  • cd with nothing after it takes you to your home folder (/root for root).
  • tree /srv/company: draw the folder and everything inside it as a tree. > saves that drawing into a file.
First start the lab machine in the terminal panel on the right.

Wildcards for paths

You'll learn wildcards properly in the next lesson, but they're handy for exploring right away. * means "any characters":

text
ls -d /etc/s*          # everything in /etc whose name starts with s
ls /var/log/*.log      # every .log file directly in /var/log

Mixing 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: /etc config, /var/log logs, /var/lib app data, /home and /root home folders, /usr/bin programs, /opt hand-installed software, /tmp temporary, /dev devices, /proc live kernel info.
  • Absolute paths start with /; relative paths start from where you are. . here, .. up, ~ home.
  • pwd shows where you are; cd moves (cd alone goes home, cd - goes back).
  • ls -l columns: type + permissions, links, owner, group, size, date, name. The first character is the type: - file, d directory, l link, c/b devices.
  • Everyday combinations: ls -lah, ls -lhS (biggest first), ls -ltr (newest last), ls -ld DIR.
Check your understanding

Quick quiz

Feedback