find: Locating Files and Directories in Linux
The find command is a powerful and versatile utility in Linux and Unix-like systems used for searching files and directories within a specified directory hierarchy. It can locate files based on a wide range of criteria, including name, type, size, modification time, permissions, and more, and then perform actions on the located files.
1. Basic Search by Name and Type
Find Files by Extension
To locate files with a specific extension within a given directory and its subdirectories.
find <start_location> -type f -name "*.log"
# Example: Find all .log files starting from the current directory
find . -type f -name "*.log"find <start_location>: The directory wherefindwill begin its search..refers to the current directory.-type f: Specifies that we are searching for files (f). Use-type dfor directories.-name "*.log": Matches filenames ending with.log. The*is a wildcard. Use quotes around patterns to prevent shell expansion.
2. Combining Search Conditions (AND, OR, NOT)
find allows you to combine multiple search criteria using logical operators. By default, multiple conditions are combined with an AND operator. You can explicitly use AND (-a), OR (-o), and NOT (!). Conditions within parentheses must be escaped (\( ... \)) and require a preceding space.
Use the OR Statement
To find files that match any of several specified names.
find <start_location> -type f \( -name "file1.log" -o -name "file2.log" -o -name "file3.log" \)
# Example: Find specific log files in /var/log
find /var/log -type f \( -name "syslog" -o -name "auth.log" \)\( ... \): Groups conditions together. The parentheses need to be escaped for the shell.-o: Logical OR operator.-name "filename": Matches exact filenames.
Use the AND Statement (Implicit or Explicit)
# Implicit AND (default behavior)
find . -type f -name "*.txt" -size +1M
# Explicit AND
find . -type f -a -name "*.txt" -a -size +1MUse the NOT Statement (!)
# Find all files NOT ending with .txt
find . -type f ! -name "*.txt"3. Search by Modification Time
To find files that were modified within a specific timeframe.
find . -newermt "2026-01-23 09:00:00" ! -newermt "2026-01-23 11:00:00" -ls-newermt "YYYY-MM-DD HH:MM:SS": Finds files modified more recently than the specified time.!: Logical NOT.-ls: Prints the name of the current file with itsstat(2)data (likels -lids).
This command finds files modified after 09:00:00 on 2026-01-23 AND not modified after 11:00:00 on 2026-01-23. Effectively, it finds files modified between 09:00:00 and 11:00:00 on that date.
4. Executing Commands on Found Files (-exec)
You can perform actions on the files found by find using the -exec option.
Delete Found Files
# Delete all .bak files older than 7 days
find . -type f -name "*.bak" -mtime +7 -exec rm {} \;-mtime +7: Files modified more than 7 days ago.-exec <command> {} \;: Executes the specified command.{}is a placeholder for the current file path, and\;terminates the command.
Change Permissions
# Change permissions of all HTML files to 644
find . -type f -name "*.html" -exec chmod 644 {} \;Note: Be careful with find commands, especially when using -exec with commands like rm or chmod, as they can cause irreversible changes. Always test with a safer command like echo first (e.g., find . -name "*.bak" -exec echo {} \;) before executing destructive actions.