Skip to content
Laviyx Learn

Try

Linux and Bash course menu

Linux find Command with Examples

Find files by name, type, size and age with the find command, run actions with -exec and -delete, handle file names safely with xargs -0, and use locate.

Beginner6 min readUpdated
On this page

grep searches inside files. find searches for the files themselves, by name, type, size, age and more.

The form is find where conditions:

Terminal
find app
Output
app
app/logs
app/logs/small.log
app/logs/big.log
app/README.md
app/build
app/build/empty.tmp
app/src
app/src/util.py
app/src/main.py

By name#

Terminal
find app -name "*.py"
find app -iname "readme*"
Output
app/src/util.py
app/src/main.py
app/README.md

-iname ignores case. Always quote the pattern, so that the shell does not expand it first.

By type#

-type f finds files, and -type d finds folders:

Terminal
find app -type d
Output
app
app/logs
app/build
app/src

By size and age#

Terminal
find app -type f -size +1M
find app -type f -mtime +30
find app -type f -empty
Output
app/logs/big.log
app/logs/small.log
app/build/empty.tmp
ConditionMeaning
-size +100MBigger than 100 MB
-size -1kSmaller than 1 KB
-mtime +30Modified more than 30 days ago
-mtime -1Modified in the last 24 hours
-mmin -10Modified in the last 10 minutes
-maxdepth 1Do not go deeper than this level

Combining conditions#

Conditions are joined with "and" by default. Use -o for "or", and ! for "not":

Terminal
find app -type f \( -name "*.log" -o -name "*.tmp" \)
find app -type f ! -name "*.py" -path "*src*"
find app -maxdepth 1 -type f
Output
app/logs/small.log
app/logs/big.log
app/build/empty.tmp
app/README.md

Do something with the results#

-exec runs a command on each match. {} stands for the file, and \; ends the command:

Terminal
find app -name "*.py" -exec wc -l {} \;
Output
1 app/src/util.py
1 app/src/main.py

Ending with + passes many files to one command, which is faster:

Terminal
find app -name "*.py" -exec wc -l {} +
Output
1 app/src/util.py
1 app/src/main.py
2 total

To delete, use -delete. Always run without it first, to see what would be removed:

Terminal
find app -name "*.tmp"
find app -name "*.tmp" -delete
find app -name "*.tmp" | wc -l
Output
app/build/empty.tmp
0

find with xargs#

File names can contain spaces. -print0 and xargs -0 separate names with a null character, so they are always safe:

Terminal
touch "app/src/my notes.txt"
find app -name "*.txt" -print0 | xargs -0 ls -l
Output
-rw-rw-r-- 1 asha asha 0 Nov  5 06:24 app/src/my notes.txt

Hide permission errors#

Searching the whole system prints many "Permission denied" lines. Discard them:

Terminal
find / -name "hostname" -type f 2> /dev/null | head -n 2
Output
/usr/bin/hostname
/usr/lib/byobu/hostname

locate#

locate searches a prebuilt database, so it is very fast, but it may be out of date. Install it with sudo apt install plocate, and update it with sudo updatedb.

Common mistakes#

Practice#

Practice exercise

Print only the names of the two largest files under app, with their sizes in bytes.

Show hint

find -printf '%s %p\n' prints size and path. Then use sort -nr and head.

Show solution
Terminal
find app -type f -printf '%s %p\n' | sort -nr | head -n 2
Output
2000000 app/logs/big.log
2 app/src/util.py

Key takeaway#

Next: sort, uniq, cut, tr and wc.

Finished this lesson?