The find command

search for files in a directory hierarchy 36 examples from the lessons of the IVski Linux Playground — try them in the terminal next to them.

Options in the examples

-name pattern
Base of file name (the path with the leading directories removed) matches shell pattern pattern.
-type c
File is of type c:
-size n[cwbkMG]
File uses less than, more than or exactly n units of space, rounding up.
-mtime n
File's data was last modified less than, more than or exactly n*24 hours ago.
-perm mode
File's permission bits are exactly mode (octal or symbolic).
-exec command ;
Execute command; true if 0 status is returned.
-delete
Delete files or directories; true if removal succeeded.
-iname pattern
Like -name, but the match is case insensitive.
-newer reference
Time of the last data modification of the current file is more recent than that of the last data modification of the reference file.
-not expr
Same as ! expr, but not POSIX compliant.
-print0
True; print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses).

Examples

find / -type f -size +100M 2>/dev/null
find [where] [what] [action]
find . -name "*.txt"

.txt files in current dir (recursive)

find / -name "config.php"

Find config.php anywhere

find . -iname "*.TXT"

Case-insensitive

find . -name "log*"

Files starting with 'log'

find . -type f

Files only

find . -type d

Directories only

find . -type l

Symlinks only

find / -size +100M

Larger than 100MB

find / -size -1k

Smaller than 1KB

find . -size +10M -size -100M

Between 10MB and 100MB

find . -mtime -7

Modified in last 7 days

find . -mtime +30

Modified more than 30 days ago

find . -newer reference.txt

Newer than reference.txt

find . -perm 777

Exact permissions 777

find . -perm -u+x

Owner has execute

find / -perm -4000

Setuid files

find . -name "*.log" -delete

Delete found files

find . -name "*.txt" -exec cat {} \;

Run command on each

find . -name "*.sh" -exec chmod +x {} +

Make scripts executable

find . -type f -exec ls -la {} +

List found files

find . -name "*.log" -size +10M

AND

find . -name "*.log" -o -name "*.tmp"

OR

find . -not -name "*.txt"

NOT

find . -name "*.txt" | xargs cat

Cat all .txt files

find . -name "*.log" | xargs rm

Delete all .log files

find . -name "*.txt" -print0 | xargs -0 rm
find . -name "*.jpg" | xargs -P 8 -I{} convert {} {}.png
find . -name "*.txt" | xargs -I{} cp {} /backup/{}
find /var/log -name '*.log' -mtime +7 | xargs gzip
find . -name '*.py' | xargs wc -l
find /etc -name '*.conf' | xargs grep -l 'ServerName'
find / -size +100M -type f

Individual large files

find / -perm -4000 -type f 2>/dev/null

Setuid files (privilege-escalation risk)

find /home -mtime -1 -type f

Files modified in the last 24 hours

Lessons that use find

Related commands