$ run as a normal user# run as root (or with sudo)
Skills in this chapter
- Find files by name, size, time and rights, and act on them
Find files fast with locate
locate searches a database of all file names. It finds names that contain the text.
locate bin/zip
/usr/bin/zip
/usr/bin/zipcloak
/usr/bin/zipnote
/usr/bin/zipsplit
locate zip | grep bin
sudo updatedb- The database is updated once a day by a timer or cron job (
updatedb). Files made today may not be in it yet: runsudo updatedb. - There are several versions:
mlocate,plocate(fastest, new systems). They work the same way.
Search the real files with find
find walks through a directory tree and prints every file that passes all the tests.
Use the find tests
Tests check name, size, time, owner and rights. A + means "more than", a - "less than", nothing means "exactly".
| Test | Matches files … |
|---|---|
-name pattern | whose name matches (wildcards) |
-iname pattern | the same, ignoring case |
-size n | of size n: c bytes, k KB, M MB, G GB (+1M = bigger than 1 MB) |
-mtime n | whose contents changed n×24 hours ago (-7 = in the last 7 days) |
-mmin n | changed n minutes ago |
-cmin n / -ctime n | whose status (rights, owner) changed |
-newer file | changed after file |
-user name / -group name | owned by that user / group |
-nouser / -nogroup | whose owner / group no longer exists |
-perm mode | with exactly these rights (-perm -4000 = setuid bit set) |
-empty | empty files and directories |
-inum n | with inode number n (finds all hard links) |
-samefile name | the same file as name |
find /etc -mmin -60
find ~ -empty
sudo find / -perm -4000 -type f 2> /dev/null
sudo find /home -nouserAt work
find / -perm -4000 lists all setuid programs. Run it on new servers: an unknown setuid file is a security warning.
Combine tests with operators
Tests are joined with and by default. -or, -not and parentheses build more complex rules.
| Operator | Meaning |
|---|---|
-and / -a | Both must be true (default) |
-or / -o | One must be true |
-not / ! | Not true |
\( \) | Group tests (escape the parentheses for the shell) |
find ~ \( -type f -not -perm 0600 \) -or \( -type d -not -perm 0700 \)This finds files without mode 600 or directories without mode 700: good for checking a private folder.
Run actions on the results
Actions do something with each result. -print is the default.
| Action | What it does |
|---|---|
-print | Print the full path (default) |
-ls | Print like ls -dils |
-delete | Delete the file |
-quit | Stop after the first match |
-exec cmd {} ';' | Run a command; {} is the file name |
-ok cmd {} ';' | The same, but ask first |
-exec cmd {} + | Run the command once with many names (faster) |
find ~ -type f -name "*.bak" -print
find ~ -type f -name "*.bak" -delete
find ~ -type f -name "foo*" -exec ls -l '{}' ';'
find ~ -type f -name "foo*" -ok rm '{}' ';'
find ~ -type f -name "foo*" -exec ls -l '{}' +Watch out
Order matters. find ~ -print -type f prints everything, because -print comes before the test. Test with -print before you use -delete.
Pass results to xargs
xargs reads names from stdin and builds a command line from them. It is like -exec … +.
find ~ -type f -name "foo*" -print | xargs ls -l
find ~ -iname "*.jpg" -print0 | xargs -0 ls -l
find /var/log -name "*.log" -mtime +30 | xargs -r gzip- Names with spaces or new lines break normal
xargs. Usefind -print0andxargs -0: the names are separated by a null byte. xargs -r(--no-run-if-empty) does nothing when there is no input.
Practise in a playground
Make many test files and practise find with touch, which makes empty files and changes file times.
mkdir -p playground/dir-{001..100}
touch playground/dir-{001..100}/file-{A..Z}
find playground -type f -name "file-A" | wc -l
100
touch playground/timestamp
touch playground/dir-*/file-B
find playground -type f -newer playground/timestamp | wc -l
100
find playground \( -type f -not -perm 0600 \) -exec chmod 0600 '{}' ';'
find playground \( -type d -not -perm 0700 \) -exec chmod 0700 '{}' ';'Use the find options
Options change how find walks the tree.
| Option | Meaning |
|---|---|
-depth | Handle the files before their directory |
-maxdepth n | Go down at most n levels |
-mindepth n | Start testing n levels down |
-mount / -xdev | Do not go into other filesystems |
-noleaf | For filesystems that do not follow Unix rules (CD, Windows) |
Summary of the chapter
locate for speed, find for exact rules + actions, xargs -0 for safe pipelines.
1Why does locate not find a file you made five minutes ago?
Its database was built earlier. Run sudo updatedb.
2Which command lists files in /var/log bigger than 100 MB?
3Which is faster: -exec cmd {} ';' or -exec cmd {} +?
+: it runs the command once with many names instead of once per file.