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

Source: https://aminbytes.dev/bytes/devices-and-filesystems/find-system-files/


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 |

 (Side note: 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 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:

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

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

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

```bash {title="/etc/updatedb.conf"}
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**:

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

 (Side note: 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?

| 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

- `/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

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?
   - `/usr/bin`
   - **`/usr/local/bin`** (correct)
   - `/opt/bin`
   `/usr/local` is for software the local admin installs. `/usr/bin` belongs to the package manager, which may overwrite files there.

2. You created `/srv/report.txt` a minute ago, but `locate report.txt` doesn't find it. Why?
   - locate only searches `/usr`
   - **The locate database is out of date: run `updatedb`** (correct)
   - locate ignores `.txt` files
   locate searches a database, not the disk. The database is usually rebuilt once a day, so new files appear only after `updatedb` runs.

3. `ls` is an alias on your system. Which command tells you that?
   - `which ls`
   - `whereis ls`
   - **`type ls`** (correct)
   `type` is a Bash builtin, so it knows about aliases, functions and builtins. `which` and `whereis` only look at files on disk.


