-
Notifications
You must be signed in to change notification settings - Fork 19
Ultimate Linux Find Command Cheatsheet
The find command is a powerful Linux utility to search files and directories based on name, type, size, permissions, modification time, and content.
It can also take actions on search results like deleting, moving, or executing commands.
find [path] [options] [expression]
-
path β Starting point (
.= current directory,/= root, or any path). -
options β Filters, depth control, etc.
-
expression β Criteria for file selection (
-name,-type,-size, etc.).
π Example:
find /home/user -type f -name "*.txt"
Finds all .txt files in /home/user.
| Option | Description | Example |
|---|---|---|
| -name pattern | Search by file name (case-sensitive) | find . -name "file.txt" |
| -iname pattern | Case-insensitive search | find . -iname "file.TXT" |
| -type f/d/l | File type: f=file, d=directory, l=symlink | find . -type d |
| -size [+/-]n | File size search (+ larger, - smaller, k/M/G) | find . -size +10M |
| -mtime n | Modified n days ago | find . -mtime -7 |
| -perm mode | File permissions | find . -perm 644 |
| -user name | Files owned by a user | find / -user root |
| -empty | Empty files/directories | find . -empty |
| -maxdepth n | Limit search depth | find . -maxdepth 1 -type f |
| -mindepth n | Minimum search depth | find . -mindepth 2 -type f |
| -exec cmd {} ; | Execute a command per file | find . -name "*.log" -exec rm {} ; |
| -exec cmd {} + | Execute command in batch | find . -name "*.log" -exec rm {} + |
| -delete | Delete matching files ( |
find . -name "*.tmp" -delete |
find ./GFG -name "sample.txt"
Finds sample.txt in the GFG directory.
find ./GFG -name "*.txt"
Finds all .txt files.
π‘ Tip: Always quote *.txt to prevent shell expansion.
find ./GFG -name "sample.txt" -exec rm -i {} \;
Prompts before deleting each file.
find ./GFG -empty
Lists empty files and directories.
find ./GFG -perm 664
Finds files with 664 permissions.
find . -type d
Lists all directories recursively.
find ./ -type f -name "*.txt" -exec grep "Geek" {} \;
Finds files containing "Geek".
find /path/to/search -mtime -7
Files modified in the last 7 days.
find . -type f -exec grep -l "pattern" {} \;
Shows filenames containing "pattern".
Start Searching
|
v
[Specify Path] ----------------------.
| |
v v
Current Dir (.) Any Dir (/path)
|
v
[Choose File Type]
|
|-- f (regular file)
|-- d (directory)
|-- l (symlink)
|
v
[Add Criteria / Expression]
|
|-- -name "pattern" --> Search by exact name
|-- -iname "pattern" --> Case-insensitive
|-- -size [+/-]n --> Search by size
|-- -mtime n --> Search by modification time
|-- -perm mode --> Search by permissions
|-- -user username --> Search by owner
|-- -empty --> Empty files/dirs
|
v
[Depth Control - Optional]
|
|-- -maxdepth n
|-- -mindepth n
|
v
[Action on Results]
|
|-- -print --> Display file paths
|-- -exec command {} \; --> Execute per file
|-- -exec command {} + --> Execute in batch
|-- -delete --> Delete files
|
v
[Advanced Combination]
|
|-- Combine with grep
| Example: find . -type f -exec grep -l "pattern" {} \;
|
v
[End]
β find . -name *.txt β Use quotes: find . -name "*.txt"
β Searching root / without -maxdepth β slow
β Using -delete without testing β risky
β Always test with -print first!
-
Find all empty directories inside
/var/log. -
Find files owned by root modified in the last 2 days.
-
Delete all files larger than 100MB inside
/tmp. -
List all
.shfiles in the current directory (non-recursive). -
Search for
"TODO"comments inside.pyfiles.
-
Use
-exec ... {} +for batch execution (faster than\;). -
Combine with
xargsfor performance:
find . -name "*.log" | xargs rm
-
Use
locatefor very fast searches (requiresupdatedb). -
Combine multiple criteria:
find . -type f -name "*.txt" -size +1M -mtime -7
-
Difference between
findandlocate? -
How does
findhandle case-insensitive search? -
How to delete only empty files?
-
Which is faster:
findorgrep -rfor content search?