Random Musings

Sporadic thoughts on tech, economics, business, finance and trading

find command examples

,


Basic File Search

  1. Find all files in a directory
    find /path/to/dir -type f
    Find all regular files.
  2. Find all directories
    find /path/to/dir -type d
    Find all directories.
  3. Find files by name
    find /path/to/dir -name "file.txt"
    Search for files with the exact name file.txt.
  4. Case-insensitive name search
    find /path/to/dir -iname "file.txt"
    Find files named file.txt, ignoring case.
  5. Find files by extension
    find /path/to/dir -name "*.txt"
    Search for all .txt files.

Search by File Size

  1. Files larger than 10MB
    find /path/to/dir -size +10M
  2. Files smaller than 500KB
    find /path/to/dir -size -500k
  3. Files exactly 1GB
    find /path/to/dir -size 1G

Search by Time

  1. Find files modified in the last 7 days
    find /path/to/dir -mtime -7
  2. Find files modified more than 30 days ago
    find /path/to/dir -mtime +30
  3. Find files accessed in the last 24 hours
    find /path/to/dir -atime -1
  4. Find files with a specific modification time
    find /path/to/dir -newermt "2024-11-01"
    Find files modified after a specific date.

User and Permissions

  1. Find files owned by a specific user
    find /path/to/dir -user username
  2. Find files owned by a group
    find /path/to/dir -group groupname
  3. Find files with specific permissions
    find /path/to/dir -perm 644
  4. Find executable files
    find /path/to/dir -perm /111

Combining Conditions

  1. Find .txt files modified in the last 7 days
    find /path/to/dir -name "*.txt" -mtime -7
  2. Find .log files larger than 1MB
    find /path/to/dir -name "*.log" -size +1M
  3. Find files owned by a user and writable
    find /path/to/dir -user username -perm -u=w

Actions on Found Files

  1. Delete files matching a condition
    find /path/to/dir -name "*.tmp" -delete
  2. Move found files
    find /path/to/dir -name "*.log" -exec mv {} /new/path \;
  3. Change permissions
    find /path/to/dir -type f -exec chmod 644 {} \;
  4. Print only file names
    find /path/to/dir -type f -print

Symbolic Links

  1. Find symbolic links
    find /path/to/dir -type l
  2. Follow symbolic links during search
    find /path/to/dir -L -name "*.txt"

Depth and Scope

  1. Limit search depth
    find /path/to/dir -maxdepth 2 -name "*.txt"
  2. Search only current directory
    find /path/to/dir -maxdepth 1 -type f
  3. Exclude specific directory
    find /path/to/dir -path "/path/to/dir/exclude" -prune -o -name "*.txt" -print

Advanced Examples

  1. Find empty files
    find /path/to/dir -type f -empty
  2. Find files matching multiple patterns
    find /path/to/dir \( -name "*.txt" -o -name "*.log" \)
  3. Find recently created files
    find /path/to/dir -ctime -7
    Files created in the last 7 days.
  4. Count files
    find /path/to/dir -type f | wc -l

Examples for Special Cases

  1. Find files by inode
    find /path/to/dir -inum 123456
  2. Find files with specific file system type
    find /path/to/dir -fstype ext4
  3. Find broken symbolic links
    find /path/to/dir -xtype l