Advanced
Searching inSummaries
How to search
  • Words find lesson titles, text and code. Filter by type (guides, courses, programs, books) or stay in this book.
  • book:rhcsa and ch:8 narrow the search; #code finds a word only in commands of the lessons.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Chapter 17Name in the book: Searching for Files

Find files with locate and find

A Linux system has hundreds of thousands of files. Two tools find them: locate is very fast but uses a database, and find searches the real directories with many tests and can run actions on every result. Together with xargs, find becomes one of the most powerful commands of all.

  • 9 Lessons
  • 10 Commands
  • 1 Labs

$ 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

17.1

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: run sudo updatedb.
  • There are several versions: mlocate, plocate (fastest, new systems). They work the same way.

17.2

Search the real files with find

find walks through a directory tree and prints every file that passes all the tests.

find ~ | wc -l
find ~ -type d | wc -l
find ~ -type f -name "*.jpg" -size +1M | wc -l
-typeMeaning
bBlock device
cCharacter device
dDirectory
fRegular file
lSymbolic link

Remember

Put the pattern of -name in quotes: -name "*.jpg". Without quotes, the shell expands * first.

17.3

Use the find tests

Tests check name, size, time, owner and rights. A + means "more than", a - "less than", nothing means "exactly".

TestMatches files …
-name patternwhose name matches (wildcards)
-iname patternthe same, ignoring case
-size nof size n: c bytes, k KB, M MB, G GB (+1M = bigger than 1 MB)
-mtime nwhose contents changed n×24 hours ago (-7 = in the last 7 days)
-mmin nchanged n minutes ago
-cmin n / -ctime nwhose status (rights, owner) changed
-newer filechanged after file
-user name / -group nameowned by that user / group
-nouser / -nogroupwhose owner / group no longer exists
-perm modewith exactly these rights (-perm -4000 = setuid bit set)
-emptyempty files and directories
-inum nwith inode number n (finds all hard links)
-samefile namethe same file as name
find /etc -mmin -60
find ~ -empty
sudo find / -perm -4000 -type f 2> /dev/null
sudo find /home -nouser

At work

find / -perm -4000 lists all setuid programs. Run it on new servers: an unknown setuid file is a security warning.

17.4

Combine tests with operators

Tests are joined with and by default. -or, -not and parentheses build more complex rules.

OperatorMeaning
-and / -aBoth must be true (default)
-or / -oOne 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.

Commands in this lesson

17.5

Run actions on the results

Actions do something with each result. -print is the default.

ActionWhat it does
-printPrint the full path (default)
-lsPrint like ls -dils
-deleteDelete the file
-quitStop 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.

17.6

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. Use find -print0 and xargs -0: the names are separated by a null byte.
  • xargs -r (--no-run-if-empty) does nothing when there is no input.

17.7

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 '{}' ';'

17.8

Use the find options

Options change how find walks the tree.

OptionMeaning
-depthHandle the files before their directory
-maxdepth nGo down at most n levels
-mindepth nStart testing n levels down
-mount / -xdevDo not go into other filesystems
-noleafFor filesystems that do not follow Unix rules (CD, Windows)
find /etc -maxdepth 1 -name "*.conf"
sudo find / -xdev -size +500M

17.9

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.

Practice

All commands in this chapter