walk a file hierarchy

Find lets you filter all files in a directory by their properties (name, age, last modified, owner, ...) and perform actions on the results.

Tips

Especially when using -exec, it's wise to list the files to be operated on first, as debugging by looking at removed files is usually painful!

Examples

Find all directories directly under /var/log matching foo* which are older than 7 days, and delete them:

find /var/log -name "foo*" -mtime +7 -maxdepth 1 -exec rm -rf {} +

Search for files matching foo* or bar under /var/log and delete them:

find /var/log \\( -name "foo*" -o -name "bar" \\) -type f -exec rm -rf {} +

Flags

-exec allows you to execute a command against every result. It's usage is a little unintuitive, so here's a refresher

-exec rm -rf {} +