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

View .md

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:

DirectoryWhat goes there
/the root of the tree; everything starts here
/binessential commands for all users (ls, cp, bash)
/sbinessential system commands for the admin (fsck, ip, reboot)
/bootthe kernel, initramfs and boot loader files
/devdevice files (/dev/sda, /dev/null)
/etchost-specific configuration, plain text
/homeusers’ home directories
/rootroot’s home directory (not under /home, so it’s there even if /home won’t mount)
/lib, /lib64shared libraries and kernel modules needed by /bin and /sbin
/mediamount points for removable media (USB sticks, DVDs)
/mnttemporary mounts by the admin
/optadd-on application packages, each in /opt/<package>
/procvirtual filesystem with process and kernel info
/sysvirtual filesystem with devices and drivers
/runruntime data since boot: PID files, sockets. Cleared at every boot
/srvdata this machine serves: websites, FTP, Git repositories
/tmptemporary files; may be wiped at boot
/usrthe bulk of installed software, read-only in normal use
/varvariable data that changes while running

Inside /usr and /var:

DirectoryWhat goes there
/usr/bin, /usr/sbinmost user and admin programs
/usr/liblibraries for those programs
/usr/sharearchitecture-independent data: man pages (/usr/share/man), docs, icons, zoneinfo
/usr/includeC header files
/usr/srcsource code, such as kernel sources
/usr/localsoftware the local admin installs: has its own bin, sbin, lib, share, etc
/var/loglog files
/var/libstate data of programs: package databases, /var/lib/mysql
/var/cachecaches that can be regenerated, like downloaded packages
/var/spoolqueues: print jobs, mail, cron tables (/var/spool/cron)
/var/mailusers’ mailboxes
/var/tmptemporary files that survive a reboot
On current Debian, Ubuntu, RHEL and Fedora, /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 havePut 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:

bash
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
console
$ 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:

console
$ 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.

OptionDoes
-iignore case
-conly count the matches
-bmatch the base name only, not the whole path
-eonly show files that still exist
-l Nstop after N results
-rthe 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:

bash
sudo updatedb

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

/etc/updatedb.confbash
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"
VariableSkips
PRUNEFSfilesystem types (network and virtual filesystems)
PRUNEPATHSdirectory paths
PRUNENAMESdirectory names wherever they appear
PRUNE_BIND_MOUNTSbind 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:

console
$ 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:

console
$ 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.

On RHEL and Fedora, 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.

console
$ 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?#

ToolSearchesFresh?Good for
findthe live directory treealwaysanything by name, size, owner, age, type
locatea database from updatedbas of the last updatefast name search on the whole disk
whereisstandard directories, PATH and MANPATHalwaysbinary + man pages + source of a command
whichyour PATHalwayswhich program file a command name runs
typethe shell itself, then PATHalwaysaliases, builtins, functions and files

Exam checklist#

  • /etc config, /var variable data (/var/log, /var/lib, /var/spool, /var/cache), /usr read-only software, /usr/share shared data and man pages.
  • /usr/local for locally installed software, /opt/<package> for add-on bundles, /srv for served data.
  • /media removable media, /mnt temporary mounts, /run runtime data, /tmp wiped at boot, /var/tmp kept.
  • /root is root’s home. /proc and /sys are virtual. /boot holds the kernel and initramfs.
  • find searches live and is slow. locate searches a database and is fast but may be stale: updatedb refreshes it.
  • /etc/updatedb.conf: PRUNEFS, PRUNEPATHS, PRUNENAMES, PRUNE_BIND_MOUNTS.
  • whereis finds binary, man page and source (-b, -m, -s). which prints the first match in PATH (-a for all).
  • type is a Bash builtin and the only one of these that knows aliases, functions, keywords and builtins.

Byte check

0/3

Answer these 3 questions to commit this byte to your learning path.

  1. You wrote a script that every user on the machine should be able to run. It isn’t part of any package. Where does the FHS say it belongs?
  2. You created /srv/report.txt a minute ago, but locate report.txt doesn’t find it. Why?
  3. ls is an alias on your system. Which command tells you that?
Search posts and bytes

Optional account

Keep your progress everywhere

Everything on AminBytes is free to read, with or without an account. Sign in only if you want your commits, streak and merged topics saved, on every device.

or

We keep only your name, avatar and which bytes you finished, and you can delete it all anytime. Privacy