Command linehardBash

Advanced Find Command Examples

Powerful find command usage for searching files with complex criteria and actions.

01

The problem

You need to find files based on multiple criteria, perform batch operations, or clean up old files.

02

The solution

Bash
# Basic find by name
find /path -name "filename.txt"
find . -iname "*.jpg"           # Case insensitive

# Find by type
find /path -type f              # Files only
find /path -type d              # Directories only
find /path -type l              # Symbolic links

# Find by size
find /path -size +100M          # Larger than 100MB
find /path -size -10k           # Smaller than 10KB
find . -size +50M -size -100M   # Between 50MB and 100MB

# Find by time
find /path -mtime -7            # Modified in last 7 days
find /path -mtime +30           # Modified more than 30 days ago
find /path -atime -1            # Accessed in last 24 hours
find /path -ctime +60           # Status changed >60 days ago
find /path -newer reference.txt # Newer than reference file

# Find by permissions
find /path -perm 644            # Exact permissions
find /path -perm /u=x           # Executable by owner
find /path -perm /g+w           # Writable by group
find /path -perm -o=r           # Readable by others

# Find by owner
find /path -user username
find /path -group groupname
find /path -uid 1000
find /path -gid 100

# Complex conditions
find /path \( -name "*.tmp" -o -name "*.log" \)
find . -name "*.jpg" ! -name "*thumb*"
find . -type f -empty            # Empty files
find . -type d -empty            # Empty directories

# Execute commands on found files
find /path -name "*.tmp" -delete
find /tmp -type f -atime +7 -exec rm {} \;
find . -name "*.txt" -exec cp {} /backup/ \;
find . -name "*.js" -exec chmod 644 {} \;

# Find and archive
find /var/log -name "*.log" -mtime +30 | tar czf oldlogs.tar.gz -T -

# Find text in files
find . -type f -name "*.php" -exec grep -l "mysql_connect" {} \;

# Find duplicates by content
find . -type f -exec md5sum {} \; | sort | uniq -w32 -d

# Find broken symlinks
find /path -type l -exec test ! -e {} \; -print
find /path -xtype l              # Broken symlinks

# Find and prompt before delete
find . -name "*.bak" -ok rm {} \;

# Find with depth control
find . -maxdepth 2 -name "*.conf"
find . -mindepth 3 -type f

# Find and format output
find . -type f -printf "%p\t%s\t%T+\n"
find /var -type f -exec ls -lh {} \;

03

Put it to work

Example
# Clean old temporary files
find /tmp -type f -atime +7 -delete

# Find large media files for cleanup
find ~ -type f \( -name "*.mp4" -o -name "*.avi" -o -name "*.mkv" \) -size +500M

# Fix permissions on web files
find /var/www -type f -exec chmod 644 {} \;
find /var/www -type d -exec chmod 755 {} \;

# Find PHP files containing specific function
find . -name "*.php" -exec grep -l "deprecated_function" {} \;

# Backup config files modified today
find /etc -type f -name "*.conf" -mtime -1 -exec cp {} /backup/ \;

# Find and count files by extension
find . -type f -name "*.js" | wc -l

# Find world-writable files (security check)
find / -type f -perm -0002 ! -type l -exec ls -l {} \; 2>/dev/null

Worth knowing

Use -exec for actions, {} is replaced by filename. ; ends -exec (escape as \;). -ok prompts before action. Combine criteria with -a (and, default), -o (or), ! (not). Test with -print before -delete.