Find is a powerful tool for searching files and directories in a command-line interface that can also perform actions on the results.

See examples:

find .
find directory

Defining type: directory, file, or symbolic link

find . -type d
find . -type f
find . -type l

Name pattern, wildcard, case insensitive, and user:

find . -type f -name "file.txt"
find . -type f -name "file*"
find . -type f -iname "file*"
find . -type f -i -name "*.py*"
find . -user root
find . ! -user root

Find files modified less than 10 minutes ago, more than 10 minutes ago, and within a range:

find . -type f -mmin -10
find . -type f -mmin +10
find . -type f -mmin +10 -mmin -5

For days instead of minutes:

find . -type f -mtime +5

For printing the file size:

find . -type f -mmin +30 -exec ls -lh {} \; | awk '{print $5 " " $9}'

Based on a specific size:

find . -size +5k
find . -size +5M
find . -size +5G
find . -empty

Based on permissions:

find . -perm 777

Performing actions on the results:

find . -exec chown user: {} +
find . -exec chown user: {} \;
find . -exec chmod 775 {} +
find . -perm 664
find . -exec rm {} +
find . -delete
find . -print

Limiting recursion depth:

find . -maxdepth 1

Counting results:

find . | wc -l
find . | wc -w
find . | wc -m
find . | wc -c

Printing file: user, type, size, name, and last modified

find . -printf "%u %y %s %p %t \n

Searching for files that contain the “text“:

find . -type f -exec grep -H 'text' {} \; -print 2>/dev/null

Find files modified in the last 5 minutes:

find . -type f -mmin -5

Finding hidden files:

find . -type f -iname ".*"
find . -type f -iname ".*" -ls

Find files by name anywhere in the directory tree:

find | grep file_name

Search, count, and calculate the total size of files sorted by extension:

find . -name '?.' -type f -printf '%b.%f\0' | awk -F . -v RS='\0' '{s[$NF] += $1; n[$NF]++} END {for (e in s) printf "%15d %4d %s\n", s[e]*512, n[e], e}' | sort -n

BONUS

Unrelated but in the same realm of finding files, rdfind can find duplicate files quickly and efficiently [Link].

sudo apt install rdfind -y
rdfind ~/Documents

It will generate a report of all duplicate files found using the following strategies:

  • Unique inodes
  • File size
    • Hash just the first few bytes
    • Hash just the last few bytes
    • Only then, hash the whole file

Duplicates can then be replaced with hard links or deleted.

Dry-run

rdfind -dryrun true ~/Documents

Hard-links

rdfind -makehardlinks true ~/Documents

Deletion

rdfind -deleteduplicates true ~/Documents