The Linux Filesystem

Linux · Lesson 4 of 32 7 min read Updated

On Windows every drive has a letter: C:, D:, E:. Linux has no drive letters at all. Everything, from your documents to your USB stick to the settings of the web server, lives in a single tree of folders that starts at one place, called root and written as /.

In this lesson you will learn what the main folders in that tree are for, how to write paths that point into it, and why Linux says "everything is a file". Knowing the map makes every later lesson easier, because you will know where to look.

One tree, starting at /

A directory is the Linux word for a folder. At the very top is the root directory /. Every other directory hangs below it, and other disks are attached somewhere inside the tree (this is called mounting, covered in Disks & Storage).

Terminal
ls /
Output
bin   dev  home  lib64       media  opt   root  sbin  srv  tmp  var
boot  etc  lib   lost+found  mnt    proc  run   snap  sys  usr

Your list may contain a few extra entries, but these names are the same on nearly every distribution. They follow the Filesystem Hierarchy Standard (FHS), a shared agreement about what goes where.

What each directory is for

DirectoryWhat it holds
/homeOne folder per normal user, such as /home/priya. Your files go here
/rootThe home folder of the root user (not the same as /)
/etcSystem-wide configuration files: users, network, SSH, web server settings
/varData that changes while the system runs: /var/log (logs), /var/www (websites), /var/lib (databases, package data)
/tmpTemporary files; cleared on reboot. Anyone can write here
/usrInstalled programs and their files: /usr/bin (commands), /usr/lib (libraries), /usr/share (docs, icons)
/usr/localSoftware you install by hand, kept apart from the distro's packages
/optLarge third-party applications installed as one folder
/bootThe kernel and the files needed to start the computer
/devDevices shown as files, such as disks (/dev/sda) and the "black hole" /dev/null
/proc, /sysVirtual files that show live kernel information; nothing here is stored on disk
/runRuntime data for running services; cleared on reboot
/mnt, /mediaPlaces to attach extra disks; USB drives appear under /media
/srvData served by this machine, used on some servers

Three directories deserve special attention. /etc is where you change how the system behaves: almost every file in it is plain text. /var/log is where you look when something goes wrong. /home is where your own work lives, and it is usually the only place a normal user can write (apart from /tmp).

Where did /bin go?

Older Unix systems kept essential commands in /bin and /sbin, and everything else in /usr/bin and /usr/sbin. Modern distributions, including Ubuntu, have merged them: /bin is now just a symbolic link (a shortcut) to /usr/bin.

Terminal
ls -ld /bin /sbin /lib
Output
lrwxrwxrwx 1 root root 7 Apr 23 09:12 /bin -> usr/bin
lrwxrwxrwx 1 root root 8 Apr 23 09:12 /sbin -> usr/sbin
lrwxrwxrwx 1 root root 7 Apr 23 09:12 /lib -> usr/lib

The l at the start and the arrow show that each one is a link. So /bin/bash and /usr/bin/bash are the same file. You will still see #!/bin/bash in scripts, and it still works.

Absolute and relative paths

A path is the address of a file or directory. There are two kinds:

  • An absolute path starts with / and gives the full route from the root: /home/priya/Documents/marks.txt. It works from anywhere.
  • A relative path does not start with /. It is read from your current directory (the folder you are "in"). If you are in /home/priya, then Documents/marks.txt means the same file.

Three short names help you write paths:

NameMeaningExample
.The current directory./backup.sh runs a script in this folder
..The parent directory, one level upFrom /home/priya/Documents, ../Downloads is /home/priya/Downloads
~Your home directory~/notes is /home/priya/notes for Priya

Linux uses forward slashes / between names. A backslash \ means something else in the shell, so Windows-style paths do not work.

Hidden files

A file or directory whose name starts with a dot is hidden: plain ls does not show it. Hidden files are usually settings, such as ~/.bashrc (your shell settings) and ~/.ssh (your SSH keys). Add -a to see them:

Terminal
ls -a ~
Output
.   .bash_history  .bashrc  .profile  Documents  notes       projects
..  .bash_logout   .cache   .ssh      Downloads  orders.csv

Here you can also see . and .. themselves listed as entries.

Everything is a file

Linux represents almost everything as a file: documents, directories, hardware devices, even information about running programs. This means the same few tools (cat, ls, grep) work on all of them. The file command tells you what something really is, whatever its name:

Terminal
file /etc/hostname /var/log /bin
head -n 3 /proc/meminfo
Output
/etc/hostname: ASCII text
/var/log:      directory
/bin:          symbolic link to usr/bin
MemTotal:        4010456 kB
MemFree:         2311488 kB
MemAvailable:    3290412 kB

/proc/meminfo is not stored on any disk. The kernel creates its contents at the moment you read it, so the numbers are always live.

Common mistakes

  • Confusing /root with /. / is the top of the whole tree; /root is only the root user's home folder.
  • Forgetting the leading slash. etc/ssh (relative) is not /etc/ssh (absolute). If a path "does not exist", check where you are.
  • Saving your work in /tmp. It is emptied on reboot. Keep anything you care about in your home folder.
  • Using backslashes or wrong capital letters. /Home/Priya does not exist; /home/priya does.

Key points

  • Linux has one directory tree starting at /; there are no drive letters.
  • /home holds user files, /etc holds configuration, /var/log holds logs and /usr/bin holds commands.
  • Absolute paths start with /; relative paths start from the current directory.
  • . is the current directory, .. the parent and ~ your home.
  • Names starting with a dot are hidden; ls -a shows them.

Exercise

Priya is in /home/priya/projects. Write the absolute path and one relative path for each of these: her Downloads folder, the SSH configuration folder /etc/ssh, and a file todo.txt inside her notes folder. Then, on your own system, use file to check what /etc/os-release is.

Show solution
TargetAbsolute pathRelative path from /home/priya/projects
Downloads/home/priya/Downloads../Downloads
SSH settings/etc/ssh../../../etc/ssh
todo.txt/home/priya/notes/todo.txt../notes/todo.txt
Terminal
file /etc/os-release
Output
/etc/os-release: symbolic link to ../usr/lib/os-release

For /etc/ssh the absolute path is far simpler than climbing up three levels, which is why absolute paths are used for system files. On Ubuntu, /etc/os-release is a link to the real file in /usr/lib.

Quick Quiz

1. Which directory holds system-wide configuration files?

2. Which of these is an absolute path?

3. Where should you look first for log files?

4. What does .. mean in a path?

Learn Linux with a mentor

Free lessons get you started. Our Ethical Hacking & Cyber Security training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call