Finding files and where they belong

Finding files, and knowing where they should have been in the first place: find versus locate, the helper commands, and the parts of the Filesystem Hierarchy Standard the exam expects by heart.

Lesson 6 of 6 in objective 104. Devices, Linux filesystems, filesystem hierarchy standard, part of LPIC-1 Exam 101-500.

Where a file belongs, and the question each directory answers. / contains /bin (commands needed before /usr is there), /sbin (the same, for administration), /etc (configuration. Local, never shared), /usr (everything else that ships), /usr/local (what the admin built from source), /opt (self-contained third-party packages), /var (logs, spools, caches. It grows), /var/tmp (scratch that survives a reboot), /tmp (scratch that may not). / /bin commands needed before /usr is there /sbin the same, for administration /etc configuration. Local, never shared /usr everything else that ships /usr/local what the admin built from source /opt self-contained third-party packages /var logs, spools, caches. It grows /var/tmp scratch that survives a reboot /tmp scratch that may not
Where a file belongs, and the question each directory answers.

find walks, locate looks it up

find walks the tree now, so it is always current and always slower: find /home -name "*.conf" -type f, with tests for size (-size +100M), age (-mtime -7), owner (-user) and permission (-perm). -exec runs a command on each result, terminated by \; for one at a time or + to batch them, and piping to xargs is the alternative. Quote the pattern or the shell expands it first.

locate answers instantly because it reads a database rather than the filesystem — which means it can be wrong in both directions, listing files that have been deleted and missing ones created since the database was built. updatedb rebuilds it, normally from a nightly job, and /etc/updatedb.conf decides what that rebuild covers: PRUNEPATHS lists directories to leave out, PRUNEFS lists filesystem types to leave out so that network and virtual filesystems are never walked, and PRUNENAMES lists directory names to skip wherever they appear. That file is why locate can know nothing about /tmp or an NFS mount immediately after a rebuild — a miss with nothing to do with the database being stale, and the one a question sets up when it says the database was refreshed a minute ago. whereis finds the binary, source and manual page for a command; which and type answer what the shell would run.

One find line, and what each test on it is asking. find /home -name "*.conf" -mtime -7 -size +100M — part 1, /home: where the walk starts, and it is walked now; part 2, -name "*.conf": quote it, or the shell expands it first; part 3, -mtime -7: minus: changed inside the last seven days; part 4, -size +100M: plus: larger than 100 megabytes. find 1 /home 2 -name "*.conf" 3 -mtime -7 4 -size +100M 1 where the walk starts, and it is walked now 2 quote it, or the shell expands it first 3 minus: changed inside the last seven days 4 plus: larger than 100 megabytes
One find line, and what each test on it is asking.

The hierarchy, and the reasoning behind it

The FHS sorts directories by two questions: shareable or not, and static or variable. /bin and /sbin hold commands needed before other filesystems are mounted — /sbin for the ones intended for administrators — while /usr/bin and /usr/sbin hold everything else. /lib holds the libraries those early commands need. /etc is configuration, always local to the machine and never shared.

/var is for data that changes, and its subdirectories divide by how long that data is meant to last: /var/log for logs, /var/spool for work queued and waiting to be processed, /var/cache for anything that can be regenerated, /var/lib for application state that must persist. Runtime data describing the machine only since it was last booted moved out of /var/run to a top-level /run, precisely because /var may be a separate filesystem mounted too late for early boot; /run is a tmpfs, so the PID files and sockets a daemon puts there are gone after a restart, which is the whole point of them. /tmp is scratch space that may be cleared on reboot, /var/tmp scratch space that should survive one.

The rest of the tree divides by who owns what is in it. The distribution owns /bin, /sbin, /usr/bin and /usr/sbin; the local administrator owns /usr/local, which mirrors the same structure one level down — /usr/local/bin, /usr/local/sbin, /usr/local/lib, /usr/local/share — so a utility built from source belongs in /usr/local/bin, where no package upgrade can collide with it. /opt is for a third-party package that keeps its own self-contained tree under /opt/<package>, and /srv for data this machine serves. /home is user directories, /root is the superuser's own, and /boot holds the kernel. /proc, /sys and /dev are kernel interfaces rather than storage, and /media and /mnt are where removable and temporary filesystems get attached.

How early a command is needed and who it is for together decide which directory holds it. A grid of Who it is for against When it has to work. Anyone: Before /usr is mounted gives /bin; Once /usr is mounted gives /usr/bin. Administrators: Before /usr is mounted gives /sbin; Once /usr is mounted gives /usr/sbin. When it has to work → Who it is for ↓ Before /usr is mounted Once /usr is mounted Anyone /bin /usr/bin Administrators /sbin /usr/sbin
How early a command is needed and who it is for together decide which directory holds it.

Worth carrying in

find / -name "x" -type f
Walk and test. Quote the pattern; -exec … \; or | xargs to act.
find -mtime -7 -size +100M
Modified within seven days; larger than 100 megabytes.
locate / updatedb
Database lookup, and the command that rebuilds the database.
whereis cmd
Binary, source and manual page for a command.
/etc
Machine-local configuration. Never shared between hosts.
/var
Data that changes: logs, spools, caches.
/usr/local
Software installed locally from source. /opt for self-contained third-party packages.
/sbin
System binaries for administration, needed before /usr is available.
/usr/local/bin
Where a binary you compiled yourself goes. /usr/local mirrors bin, sbin, lib and share below it.
/run
A tmpfs for runtime state — PID files, sockets — replacing /var/run. Empty again after a reboot.
/etc/updatedb.conf
PRUNEPATHS, PRUNEFS and PRUNENAMES: what updatedb leaves out of the database locate reads.

What the exam does with this

Objective
104. Devices, Linux filesystems, filesystem hierarchy standard
Share of the exam
25% (the whole objective)
Questions in this lesson
11
Signed for by a person
0

Partly checked. None of the 11 questions here has been read against the cited source by a person. 11 questions have been checked against their cited clause by an automated pass — which is not the same thing, and is not a signature.

Only questions a person has signed for are used in mock exams here. That is the whole difference between the two kinds of checking above.

How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.

Drill this lesson

A lesson is one sitting: the trainer draws a short run from these questions alone and spaces the ones you get wrong.

Practise Finding files and where they belong

Questions in this lesson

Practise Finding files and where they belong

The rest of objective 104