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.
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.
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.
Worth carrying in
- find / -name "x" -type f
- Walk and test. Quote the pattern; -exec … \; or |
xargsto 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.
/optfor self-contained third-party packages. - /sbin
- System binaries for administration, needed before
/usris available. - /usr/local/bin
- Where a binary you compiled yourself goes.
/usr/localmirrors 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
updatedbleaves out of the databaselocatereads.
What the exam does with this
- locate can be stale and find never is — a question about a file created five minutes ago is testing that.
- Know the
/tmpversus/var/tmpdistinction:/tmpmay be cleared on reboot,/var/tmpis expected to survive. /optversus/usr/local: third-party self-contained packages versus locally compiled software, and a hand-built binary lands in/usr/local/bin./runis a tmpfs holding state since boot, and it is where/var/runwent. Nothing in it survives a restart, which is why a PID file belongs there.- A
locatemiss is not always a stale database./etc/updatedb.confcan have excluded the path outright.
- 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
- You type `mytool` and want to know exactly which executable file the shell will run, based on your current PATH. Which command answers that? machine-checked
- Select the TWO file placements that follow the Filesystem Hierarchy Standard. machine-checked
- locate cannot find a file you created a few minutes ago. Type the command, run as root, that rebuilds the database locate searches. machine-checked
- /var/log has filled the root filesystem. Which command lists every regular file at or below /var/log whose contents were last modified more than 30 days ago? machine-checked
- find matches several thousand files and each must be passed to gzip. You want find to pack as many path names as possible into each gzip invocation instead of starting one process per file. Which action does that? machine-checked
- In one command you want the location of the mount binary, of any source directory the system carries for it, and of its manual page. Which command reports all three? machine-checked
- You build a monitoring utility from source and install it by hand, outside the distribution's package manager, as a plain binary with no bundled tree of its own. Under the FHS, which directory should the executable be installed into? machine-checked
- A daemon needs a directory for its PID file and its control socket. The data must be writable very early in boot, and it must not survive a reboot. Which directory does current FHS practice designate for it? machine-checked
- A single find command must report regular files under /home that are larger than 100 MiB and owned by the user carol. Which THREE predicates express those three requirements? machine-checked
- Type the absolute path of the configuration file that names the directories and filesystem types updatedb leaves out of the database that locate searches. machine-checked
- A colleague runs `find /etc -name *.conf` from a working directory that happens to hold two files of its own, ntp.conf and rsyslog.conf. find prints 'paths must precede expression' and does nothing. What went wrong? machine-checked
Practise Finding files and where they belong
The rest of objective 104
- Partitions and filesystems
- Keeping filesystems intact
- Mounting and unmounting filesystems
- File permissions, ownership and disk quotas
- Hard links and symbolic links
- Finding files and where they belong — you are here