LWM-Linux/06 - Linux File Operations/File Searching (find).md
2024-09-07 09:29:54 -06:00

3.1 KiB

File Searching using find

##1. Basic Syntax: The basic syntax of the find command is: find [path] [expression] # Where [path] is the directory you want to search in, and [expression] defines the search criteria.

2. Searching by Name:

  • To find files by name:
    • find /path/to/search -name "filename" # Perform a case-sensitive search "-name"
  • Use wildcards for pattern matching:
    • find /path/to/search -name "*.txt"
  • For case-insensitive search:
    • find /path/to/search -iname "filename" # Perform a insensitive case search.

3. Searching by Type:

  • Find only directories:
    • find /path/to/search -type d
  • Find only regular files:
    • find /path/to/search -type f
  • Find symbolic links:
    • find /path/to/search -type l

4. Searching by Size:

  • Find files larger than 100MB:
    • find /path/to/search -size +100M
  • Find files smaller than 1KB:
    • find /path/to/search -size -1k
  • Find files exactly 50 bytes:
    • find /path/to/search -size 50c

5. Searching by Time:

  • Find files modified in the last 7 days:
    • find /path/to/search -mtime -7
  • Find files accessed more than 30 days ago:
    • find /path/to/search -atime +30
  • Find files whose status was changed within the last hour:
    • find /path/to/search -cmin -60

6. Searching by Permissions:

  • Find files with exact permissions:
    • find /path/to/search -perm 644
  • Find files with at least these permissions:
    • find /path/to/search -perm -644

7. Logical Operators:

  • AND operation (implicit):
    • find /path/to/search -name "*.txt" -size +1M
  • OR operation:
    • find /path/to/search \( -name "*.txt" -o -name "*.pdf" \)
  • NOT operation:
    • find /path/to/search ! -name "*.txt"

8. Actions:

  • Print results (default action):
    • find /path/to/search -name "*.txt" -print
  • Execute a command on found files:
    • find /path/to/search -name "*.txt" -exec cat {} \;
  • Delete found files (use with caution):
    • find /path/to/search -name "*.tmp" -delete

9. Limiting Depth:

  • Search only in the current directory:
    • find /path/to/search -maxdepth 1 -name "*.txt"
  • Limit search to 3 levels deep:
    • find /path/to/search -maxdepth 3 -name "*.txt"

10. Performance Optimization:

  • Use '-xdev' to stay on one filesystem:
    • find /path/to/search -xdev -name "*.txt"
  • Optimize for large directories:
    • find /path/to/search -name "*.txt" -fprintf /dev/stdout '%p\0' | xargs -0 process

11. Advanced Examples:

  • Find all empty directories:
    • find /path/to/search -type d -empty
  • Find all files owned by a specific user:
    • find /path/to/search -user username
  • Find files and compress them:
    • find /path/to/search -name "*.log" -exec gzip {} +

12. Find Alternatives:

Using locate:

  • The locate command quickly searches for files and directories by their names. Here's the basic syntax:
    locate {filename}
    
  • For example, to locate a file named resolv.conf, you can run:
    locate resolv.conf