find
Quick Reference
Command Name:
find
Category:
file management
Platform:
linux
Basic Usage:
Common Use Cases
Syntax
find [starting-point...] [expression]
Options
Option | Description |
---|---|
-name pattern | Match filenames against the shell pattern (case-sensitive) |
-iname pattern | Like -name, but case-insensitive |
-type c | File is of type c: f (regular file), d (directory), l (symbolic link), etc. |
-size n[cwbkMG] | File uses n units of space (c: bytes, w: 2-byte words, b: 512-byte blocks, k: kilobytes, M: megabytes, G: gigabytes) |
-mtime n | File's data was last modified n*24 hours ago. Use -n for less than n days, +n for more than n days |
-mmin n | File's data was last modified n minutes ago. Use -n for less than n minutes, +n for more than n minutes |
-atime n | File was last accessed n*24 hours ago |
-ctime n | File's status was last changed n*24 hours ago |
-user name | File is owned by user 'name' |
-group name | File belongs to group 'name' |
-perm mode | File's permission exactly matches the octal number mode |
-perm -mode | All permission bits in mode are set for the file |
-perm /mode | Any of the permission bits in mode are set for the file |
-exec command {} \; | Execute command on each found file/directory. {} is replaced by the current file name |
-exec command {} \+ | Like -exec, but pass multiple files to command at once, making it more efficient |
-delete | Delete files (use with caution!) |
-path pattern | File path matches pattern |
-not expr | Negate the expression |
expr1 -a expr2 | Both expr1 and expr2 are true (AND) |
expr1 -o expr2 | Either expr1 or expr2 is true (OR) |
-maxdepth n | Descend at most n directory levels |
-mindepth n | Only process files and directories at least n levels deep |
-newer file | File was modified more recently than reference file |
-empty | File or directory is empty |
Examples
How to Use These Examples
The examples below show common ways to use the find
command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.
Basic Examples:
find /home -name "*.txt"
Searches for all .txt files in the /home directory and its subdirectories. The -name option specifies the filename pattern to search for using wildcards.
find . -type f -mtime -7
Finds all files (not directories) modified in the last 7 days in the current directory and its subdirectories. The -type f option restricts the search to regular files, and -mtime -7 specifies files modified less than 7 days ago.
find /var/log -type f -size +10M
Locates all files larger than 10 megabytes in the /var/log directory. The -size +10M option specifies files whose size is greater than 10 megabytes.
find . -type d -name "src"
Finds all directories named "src" in the current directory and its subdirectories. The -type d option restricts the search to directories only.
Advanced Examples:
find . -type f -name "*.log" -exec grep "error" {} \;
Searches for all .log files in the current directory and subdirectories, then executes the grep command on each file to find lines containing "error". The -exec option allows you to run commands on each file found, with {} representing the current file.
find . -type f -name "*.jpg" -size +1M -exec cp {} /backup/ \;
Finds all JPEG files larger than 1 megabyte and copies them to the /backup/ directory. This combines multiple criteria (-name, -size) with an action (-exec).