The find command in Linux is a powerful utility to search for files and directories
within the filesystem based on various criteria such as name, size, modification
date, and permissions. Here are some more examples illustrating its versatility:
Basic Usage
------------
Find Files by Name
find /path/to/search -name "filename"
Example: Find all files named test.txt in the current directory and its
subdirectories.
find . -name "test.txt"
Find Files by Name Pattern
find /path/to/search -name "*.txt"
Example: Find all .txt files in /var/log.
find /var/log -name "*.txt"
Using Other Criteria
--------------------
Find Files by Type
find /path/to/search -type TYPE
Example: Find all directories.
find . -type d
Find Files by Size
find /path/to/search -size [+|-]SIZE
Example: Find all files larger than 100MB.
find . -size +100M
Find Files by Modification Time
find /path/to/search -mtime [+|-]N
Example: Find all files modified in the last 7 days.
find . -mtime -7
Combining Criteria
-------------------
Find Files by Name and Type
find /path/to/search -name "*.sh" -type f
Example: Find all .sh files that are regular files.
find . -name "*.sh" -type f
Find Files by Name and Size
find /path/to/search -name "*.log" -size +1G
Example: Find all .log files larger than 1GB.
find . -name "*.log" -size +1G
Performing Actions on Found Files
---------------------------------
Delete Found Files
find /path/to/search -name "*.tmp" -type f -delete
Example: Find and delete all .tmp files.
find . -name "*.tmp" -type f -delete
Execute a Command on Found Files
find /path/to/search -name "*.log" -exec COMMAND {} \;
Example: Find all .log files and display their disk usage.
find . -name "*.log" -exec du -h {} \;
Find Files and Print Detailed Information
find /path/to/search -name "*.conf" -exec ls -l {} \;
Example: Find all .conf files and list their detailed information.
find /etc -name "*.conf" -exec ls -l {} \;
Advanced Examples
------------------
Find Files with Specific Permissions
find /path/to/search -perm MODE
Example: Find all files with 755 permissions.
find . -perm 755
Find Empty Files or Directories
find /path/to/search -empty
Example: Find all empty files and directories.
find . -empty
Find Files Owned by a Specific User
find /path/to/search -user username
Example: Find all files owned by the user john.
find . -user john
Find Files by Access Time
find /path/to/search -atime [+|-]N
Example: Find all files accessed in the last 10 days.
find . -atime -10
Find Files by Inode Number
find /path/to/search -inum N
Example: Find a file with a specific inode number.
find / -inum 123456