Menu
Find system files and place files in the correct location
“The Filesystem Hierarchy Standard: what lives where, where your own files belong, and how to find anything with find, locate, whereis, which and type.”
On Linux, files aren’t scattered at random. Config is in /etc, logs are in /var/log, programs are in /usr/bin. That’s not just habit: it’s written down in the Filesystem Hierarchy Standard (FHS), and nearly every distribution follows it.
Knowing the map pays off twice. You know where to look when something breaks, and you know where to put your own files so that package updates don’t overwrite them. And when you don’t know, a handful of tools will find a file or command for you.
The FHS map#
The current version is FHS 3.0, maintained by the Linux Foundation. The directories you need to know:
| Directory | What goes there |
|---|---|
/ | the root of the tree; everything starts here |
/bin | essential commands for all users (ls, cp, bash) |
/sbin | essential system commands for the admin (fsck, ip, reboot) |
/boot | the kernel, initramfs and boot loader files |
/dev | device files (/dev/sda, /dev/null) |
/etc | host-specific configuration, plain text |
/home | users’ home directories |
/root | root’s home directory (not under /home, so it’s there even if /home won’t mount) |
/lib, /lib64 | shared libraries and kernel modules needed by /bin and /sbin |
/media | mount points for removable media (USB sticks, DVDs) |
/mnt | temporary mounts by the admin |
/opt | add-on application packages, each in /opt/<package> |
/proc | virtual filesystem with process and kernel info |
/sys | virtual filesystem with devices and drivers |
/run | runtime data since boot: PID files, sockets. Cleared at every boot |
/srv | data this machine serves: websites, FTP, Git repositories |
/tmp | temporary files; may be wiped at boot |
/usr | the bulk of installed software, read-only in normal use |
/var | variable data that changes while running |
Inside /usr and /var:
| Directory | What goes there |
|---|---|
/usr/bin, /usr/sbin | most user and admin programs |
/usr/lib | libraries for those programs |
/usr/share | architecture-independent data: man pages (/usr/share/man), docs, icons, zoneinfo |
/usr/include | C header files |
/usr/src | source code, such as kernel sources |
/usr/local | software the local admin installs: has its own bin, sbin, lib, share, etc |
/var/log | log files |
/var/lib | state data of programs: package databases, /var/lib/mysql |
/var/cache | caches that can be regenerated, like downloaded packages |
/var/spool | queues: print jobs, mail, cron tables (/var/spool/cron) |
/var/mail | users’ mailboxes |
/var/tmp | temporary files that survive a reboot |
/bin, /sbin and /lib are symlinks to their /usr versions (“merged /usr”). The FHS names still work, they just lead to the same place.The FHS also sorts data on two axes: shareable (could live on a file server for several machines, like /usr and /opt) or unshareable (belongs to this host, like /etc and /boot), and static (only changes on updates, like /usr) or variable (changes all the time, like /var). That’s the reason /var is a separate tree from /usr: you can mount /usr read-only and keep /var writable.
Where to put your own files#
The rule: never mix your files into directories that the package manager owns. Updates can overwrite them, and nobody knows where they came from.
| You have | Put it in |
|---|---|
| a script or program for all users | /usr/local/bin (admin tools: /usr/local/sbin) |
| software you built from source | /usr/local (the default --prefix of most build scripts) |
| a third-party app delivered as one bundle | /opt/<app>, config in /etc/opt/<app>, data in /var/opt/<app> |
| configuration for this machine | /etc |
| a website or files you serve | /srv |
| data your service keeps between runs | /var/lib/<app> |
| your own log file | /var/log/<app>/ |
| a scratch file for a few minutes | /tmp (or /var/tmp if it must survive a reboot) |
/usr/local/bin comes before /usr/bin in the default PATH, so your version of a command wins over the packaged one.
find: search the disk live#
find walks the directory tree right now, so it’s always up to date, but slow on a big tree. It’s covered in depth with file management. The forms you need here:
find /etc -name "*.conf" # by name (quote the pattern)
find / -iname "readme*" 2>/dev/null # case-insensitive, hide "Permission denied"
find /var/log -type f -size +100M # files bigger than 100 MiB
find /home -mtime -2 # changed in the last 2 days
find / -user bob -type f # files owned by bob
find /usr/local -maxdepth 2 -type d # only two levels deep
find /tmp -name "*.tmp" -mtime +7 -delete$ find /etc -maxdepth 1 -name "*tab"
/etc/fstab
/etc/crontab
/etc/mtab
locate and updatedb: search a database#
locate doesn’t look at the disk at all. It searches a database of file names, which makes it almost instant:
$ locate fstab
/etc/fstab
/usr/lib/systemd/system-generators/systemd-fstab-generator
/usr/share/man/man5/fstab.5.gz
/usr/share/man/man8/systemd-fstab-generator.8.gz
The pattern is matched as a piece of text anywhere in the full path, so every name that merely contains “fstab” shows up too, like the systemd generator.
| Option | Does |
|---|---|
-i | ignore case |
-c | only count the matches |
-b | match the base name only, not the whole path |
-e | only show files that still exist |
-l N | stop after N results |
-r | the pattern is a regular expression |
The catch: the database is only as fresh as the last time updatedb ran. A daily cron job or systemd timer runs it for you. A file created five minutes ago won’t show up, and a deleted one still will (unless you use -e). Update it by hand when you need to:
sudo updatedbTwo implementations are around. Debian 12, Ubuntu and Fedora use plocate (database in /var/lib/plocate/). RHEL 9 still uses mlocate (/var/lib/mlocate/). Both are used the same way, and both only show you files you have permission to see. Install with apt install plocate or dnf install mlocate.
/etc/updatedb.conf#
This file tells updatedb what to skip. It’s worth knowing for the exam:
PRUNE_BIND_MOUNTS="yes"
PRUNENAMES=".git .bzr .hg .svn"
PRUNEPATHS="/tmp /var/spool /media /var/lib/os-prober /var/lib/ceph"
PRUNEFS="NFS afs autofs binfmt_misc cifs devpts iso9660 nfs nfs4 proc smbfs sysfs tmpfs udf usbfs"| Variable | Skips |
|---|---|
PRUNEFS | filesystem types (network and virtual filesystems) |
PRUNEPATHS | directory paths |
PRUNENAMES | directory names wherever they appear |
PRUNE_BIND_MOUNTS | bind mounts, so the same files aren’t indexed twice |
whereis: binary, source and manual#
whereis looks in the standard places for a command’s binary, its man pages and its source:
$ whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz
$ whereis -b passwd
passwd: /usr/bin/passwd /etc/passwd
$ whereis -m ls
ls: /usr/share/man/man1/ls.1.gz
-b limits it to binaries, -m to man pages, -s to sources. It searches a built-in list of standard directories plus your PATH and MANPATH, and shows every hit, not just the first. Notice it even found the config file /etc/passwd.
which: what runs when I type this?#
which searches your PATH in order and prints the first program with that name. That’s the one your shell would run:
$ which python3
/usr/local/bin/python3
$ which -a python3
/usr/local/bin/python3
/usr/bin/python3
Here someone built their own Python into /usr/local. Because /usr/local/bin comes earlier in PATH, that’s the one that runs. which -a shows all matches, which is how you spot two programs sharing a name.
which only knows about files. It doesn’t see aliases, shell functions or builtins like cd.
which is itself an alias set in /etc/profile.d/which2.sh that feeds your aliases into it, so there it does show aliases. Debian’s which doesn’t.type: what the shell will really do#
type is a Bash builtin, so it knows everything the shell knows: aliases, keywords, functions, builtins and files in PATH.
$ type ls
ls is aliased to `ls --color=auto'
$ type cd
cd is a shell builtin
$ type if
if is a shell keyword
$ type -a echo
echo is a shell builtin
echo is /usr/bin/echo
$ type -t ls
alias
$ type -P ls
/usr/bin/ls
-a shows every match in the order the shell tries them, -t prints a single word (alias, keyword, function, builtin or file), and -P searches PATH like which does.
Which tool when?#
| Tool | Searches | Fresh? | Good for |
|---|---|---|---|
find | the live directory tree | always | anything by name, size, owner, age, type |
locate | a database from updatedb | as of the last update | fast name search on the whole disk |
whereis | standard directories, PATH and MANPATH | always | binary + man pages + source of a command |
which | your PATH | always | which program file a command name runs |
type | the shell itself, then PATH | always | aliases, builtins, functions and files |
Exam checklist#
/etcconfig,/varvariable data (/var/log,/var/lib,/var/spool,/var/cache),/usrread-only software,/usr/shareshared data and man pages./usr/localfor locally installed software,/opt/<package>for add-on bundles,/srvfor served data./mediaremovable media,/mnttemporary mounts,/runruntime data,/tmpwiped at boot,/var/tmpkept./rootis root’s home./procand/sysare virtual./bootholds the kernel and initramfs.findsearches live and is slow.locatesearches a database and is fast but may be stale:updatedbrefreshes it./etc/updatedb.conf:PRUNEFS,PRUNEPATHS,PRUNENAMES,PRUNE_BIND_MOUNTS.whereisfinds binary, man page and source (-b,-m,-s).whichprints the first match inPATH(-afor all).typeis a Bash builtin and the only one of these that knows aliases, functions, keywords and builtins.
Answer these 3 questions to commit this byte to your learning path.
Reset. This byte isn't committed any more. Answer the questions again to commit it.
$ git commit -m "learn(lpic-104): Find system files and place files in the correct location"
[main 11a00c2] learn(lpic-104): Find system files and place files in the correct location
1 byte changed, 3 insertions(+)
See it on your path Already committed . Try the questions again any time, it won't undo anything. To uncommit it and start over: