find
Files and directories
Usage
find [path] -name '[pattern]' -type fWalks a directory tree testing every entry; quote the pattern as -name '*.log' or the shell expands it before find ever sees it.
Common flags
| Flag | Meaning |
|---|---|
| -name | Match the file name, case sensitive; -iname ignores case |
| -type | Restrict to f (file), d (directory) or l (symlink) |
| -size | Filter by size, as in -size +100M |
| -mtime | Filter by age in days, as in -mtime +7 |
| -maxdepth | Limit how deep to walk; GNU wants it before the tests |
| -delete | Delete every match, no confirmation |
| -exec | Run a command on each hit, ending with \; or + |
| -print0 | Separate results with NUL, to pair with xargs -0 |
Examples
find . -name '*.log' -mtime +7 -deleteDeletes log files older than a week.
find / -type f -size +1G 2>/dev/nullFinds files over a gigabyte, hiding permission errors.
find . -name '*.jpg' -exec mv {} photos/ +Moves every JPEG it finds.
Moving, deleting and finding. Some of these do not ask twice, which is why -i becomes a habit.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does find do?
Walks a directory tree testing every entry; quote the pattern as -name '*.log' or the shell expands it before find ever sees it.
Q. How do I type it?
find [path] -name '[pattern]' -type f — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
8 are listed here; the full set is in man find. This command sits under Files and directories.
Related commands
man page: man find