Find & Xargs Cheatsheet
find is the most powerful file search tool on Linux, and xargs is its perfect companion for batch processing. This cheatsheet organizes essential find and xargs commands by category — name/path, time, size, type, permission searches, execution, and batch actions — each with practical examples.
Quick Reference
| Task | Command |
| Search by name | find . -name "*.txt" | |
| Find recently modified files | find . -mtime -7 | |
| Find large files | find . -size +100M | |
| Execute action on results | find . -exec rm {} \; | |
| Find only directories or files | find . -type d | |
| Find by permissions | find . -perm 644 | |
| Batch process results | find . -print0 \ | xargs -0 |
Basic Search
``bash
# Search by name
find . -name "*.txt" # Exact filename match
find . -iname "readme*" # Case-insensitive
find . -name "[ab]*.py" # Glob patterns
find . -name "*.tmp" -o -name "*.log" # OR condition (-o)
# Search by path find . -path "*/src/*.c" # Match by path find . -ipath "*/.git/*" # Path case-insensitive find . -regex '.*\.\(txt\|md\)$' # Regex match on full path find . -regextype posix-egrep -regex '.*\.(txt|md)$'
# Negation find . -not -name "*.txt" # Negate result find . ! -name "*.txt" # Shorthand negation (!)
# Limit search depth find . -maxdepth 1 -name "*.txt" # Current directory only find . -mindepth 2 -name "*.txt" # Start from depth 2 find . -maxdepth 3 -mindepth 1 # Depth 1-3
# Search by inode
find . -inum 1234567 # Find by inode number
find . -samefile /path/to/file # Find hard links
`
Time Filters
`bash
# Modification time (mtime)
find . -mtime -7 # Modified in the last 7 days
find . -mtime +30 # Modified more than 30 days ago
find . -mtime 0 # Modified today
# Access time (atime) find . -atime -3 # Accessed in the last 3 days
# Status change time (ctime) find . -ctime -1 # Status changed in the last day
# Minute precision find . -mmin -60 # Modified in the last 60 minutes find . -mmin +120 # Modified more than 120 minutes ago
# Newer file comparison find . -newer reference.txt # Files newer than reference.txt find . -anewer reference.txt # Files with newer access time find . -cnewer reference.txt # Files with newer status change
# Combined time conditions
find . -mtime -7 -a -mtime +0 # In the last 7 days but not today
find . -daystart -mtime -1 # Last 1 day from midnight
`
Size Filters
`bash
# Exact size
find . -size 1024c # Exactly 1024 bytes
find . -size 1k # Exactly 1KB (1024 bytes)
# Greater than / Less than find . -size +100M # Greater than 100MB find . -size -1G # Less than 1GB find . -size +500k -a -size -1M # Between 500KB and 1MB
# Units find . -size +1G # Greater than 1GB find . -size +10M # Greater than 10MB find . -size +1000k # Greater than 1000KB find . -size +1M -a -size -10M # Between 1MB and 10MB
# Empty files / directories
find . -empty # Empty files or directories
find . -type f -empty # Empty files
find . -type d -empty # Empty directories
`
Execution
`bash
# -exec basics
find . -name "*.bak" -exec rm {} \; # Delete each file (\; terminates)
find . -name "*.log" -exec rm {} + # Batch as args (+ is faster)
# -ok with confirmation find . -name "*.tmp" -ok rm {} \; # Confirm before each deletion
# Multi-step execution find . -name "*.txt" -exec cp {} {}.back \; # Copy with .back suffix
# execdir (execute in file's directory) find . -name "*.txt" -execdir rm {} \; # Safer (runs in file's dir) find . -name "*.jpg" -execdir convert {} {}.png \; # Image conversion
# Complex commands with sh -c find . -name "*.html" -exec sh -c 'gzip "{}"' \; # Per-file execution find . -name "*.log" -exec sh -c 'mv "$1" "${1%.log}.txt"' _ {} \; # Rename
# Delete matching files
find . -name "*.swp" -delete # Direct delete (faster than -exec rm)
`
Type Filters
`bash
# File types
find . -type f # Regular files only
find . -type d # Directories only
find . -type l # Symbolic links only
find . -type s # Socket files only
find . -type p # Named pipes (FIFO)
find . -type b # Block devices
find . -type c # Character devices
# Combined types find . -type f -o -type l # Files or symlinks find . -type f -a -name "*.sh" # Regular files ending in .sh
# Links
find . -type l -ls # Find symlinks and list details
find . -xtype l # Find broken symlinks
`
Permission Search
`bash
# Exact permission match
find . -perm 644 # Exact permission 644
find . -perm 755 # Exact permission 755
# Contains permission find . -perm -u=r # Owner has read permission find . -perm -g=w # Group has write permission find . -perm -o=x # Others have execute permission
# Any user has permission find . -perm /u=r # Any user has read permission find . -perm /222 # Any user has write permission
# Owner / Group find . -user admin # Owned by user admin find . -group www-data # Belongs to group www-data find . -uid 1000 # By user ID find . -gid 100 # By group ID
# Special modes
find . -type f -perm /4000 # SUID files
find . -type f -perm /2000 # SGID files
find . -type f -perm /1000 # Sticky bit
`
Batch Operations (Xargs)
`bash
# Basic usage
find . -name "*.txt" | xargs rm # Delete .txt files (watch for spaces!)
find . -name "*.txt" -print0 | xargs -0 rm # Handle spaces in filenames
# Batch operations find . -name "*.py" -print0 | xargs -0 -I {} cp {} /backup/ # Copy to backup find . -name "*.jpg" -print0 | xargs -0 -P 4 convert -resize 50% # Parallel
# Counting and statistics find . -name "*.log" -print0 | xargs -0 wc -l # Count all log lines find . -type f -print0 | xargs -0 ls -lh # View file details
# Limit arguments per invocation find . -print0 | xargs -0 -n 10 ls -lh # Max 10 args per call
# Placeholder substitution find . -name "*.txt" -print0 | xargs -0 -I file cp file /tmp/ # Named placeholder
# Interactive processing find . -name "*.tmp" -print0 | xargs -0 -p rm # Confirm before execution
# Combined with grep find . -name "*.py" -print0 | xargs -0 grep -l "TODO" # Files containing TODO find . -type f -print0 | xargs -0 grep -c "error" | grep -v ":0$" # Files with errors ``
Basic Search(7)
| Command | Level | ||
|---|---|---|---|
find . -nameSearch files by name (supports glob patterns) | Basic | find . -name "*.txt" | |
find . -inameSearch files by name (case-insensitive) | Basic | find . -iname "readme*" | |
find . -path / -ipathSearch by path pattern | Intermediate | find . -path "*/src/*.c" | |
find . -regexMatch full path with regex | Expert | find . -regextype posix-egrep -regex '.*\.(txt|md)$' | |
find . -maxdepth / -mindepthLimit search directory depth | Intermediate | find . -maxdepth 2 -mindepth 1 -name "*.txt" | |
find . -not / !Find files that don't match (negation) | Intermediate | find . -not -name "*.txt" | |
find . -oCombine conditions with OR | Intermediate | find . -name "*.tmp" -o -name "*.log" |
Time Filter(4)
| Command | Level | ||
|---|---|---|---|
find . -mtimeFilter by modification time (day precision) | Basic | find . -mtime -7 | |
find . -mminFilter by modification time (minute precision) | Intermediate | find . -mmin -60 | |
find . -atime / -aminFilter by access time | Intermediate | find . -atime -3 | |
find . -newerFind files newer than a reference file | Intermediate | find . -newer reference.txt |
Size Filter(3)
| Command | Level | ||
|---|---|---|---|
find . -sizeFilter by file size (supports +/- | Basic | find . -size +100M | |
find . -size +100MFind files larger than 100MB | Basic | find . -size +100M | |
find . -emptyFind empty files or directories | Basic | find . -type f -empty |
Execution(6)
| Command | Level | ||
|---|---|---|---|
find . -exec {} \;Execute a command on each matching file | Basic | find . -name "*.bak" -exec rm {} \; | |
find . -exec {} +Execute command with batched arguments | Intermediate | find . -name "*.log" -exec rm {} + | |
find . -okExecute command with confirmation prompt | Intermediate | find . -name "*.tmp" -ok rm {} \; | |
find . -execdirExecute command in file's directory (safer) | Expert | find . -name "*.txt" -execdir rm {} \; | |
find . -deleteDelete matching files directly (faster than -exec rm) | Basic | find . -name "*.swp" -delete | |
find . -exec sh -cExecute complex shell commands | Expert | find . -name "*.log" -exec sh -c 'mv "$1" "${1%.log}.txt"' _ {} \; |
Type Filter(3)
| Command | Level | ||
|---|---|---|---|
find . -type fFind regular files only | Basic | find . -type f -name "*.sh" | |
find . -type dFind directories only | Basic | find . -type d -empty | |
find . -type l / -xtype lFind symlinks or broken symlinks | Intermediate | find . -xtype l |
Permission(4)
| Command | Level | ||
|---|---|---|---|
find . -permFind by exact permissions | Intermediate | find . -perm 644 | |
find . -perm -modeFind files that have the specified permission bits | Intermediate | find . -perm -u=r | |
find . -user / -groupFind files by owner or group | Intermediate | find . -user admin -group www-data | |
find . -perm /4000Find SUID/SGID/sticky bit files | Expert | find . -type f -perm /4000 |
Batch Ops (Xargs)(6)
| Command | Level | ||
|---|---|---|---|
xargs -0Xargs with NUL separator (safe for spaces) | Basic | find . -name "*.txt" -print0 | xargs -0 rm | |
xargs -I {}Use placeholder for argument substitution | Intermediate | find . -name "*.py" -print0 | xargs -0 -I {} cp {} /backup/ | |
xargs -P NExecute commands in parallel (N concurrent processes) | Expert | find . -name "*.jpg" -print0 | xargs -0 -P 4 convert -resize 50% | |
xargs -n NPass at most N arguments per invocation | Intermediate | find . -print0 | xargs -0 -n 10 ls -lh | |
xargs -pPrompt before executing each command (interactive) | Intermediate | find . -name "*.tmp" -print0 | xargs -0 -p rm | |
find . -print0 | xargs -0 grepBatch search file contents | Intermediate | find . -name "*.py" -print0 | xargs -0 grep -l "TODO" |