find is a versatile command-line utility used to search for files within a directory hierarchy based on various criteria.
Syntax:
find path -options arguments-
-name:Matches files based on their name.find . -name "*.txt" # Finds all files ending with ".txt" in the current directory and subdirectories.
-
-type:Specifies the file type (e.g.,ffor regular files,dfor directories).find . -type d -name "temp*" # Finds all directories starting with "temp" in the current directory and subdirectories.
-
-newer:Matches files modified after a given file.find . -type f -newer file.txt # Finds all regular files modified after "file.txt".
-
-iname:Case-insensitive name matching.find . -iname "myfile.txt" # Finds "myfile.txt" regardless of case.
-
-perm:Matches files based on their permissions.-u=s:Set-user-ID bit set.-g=s:Set-group-ID bit set.-u=r:Read permission for the owner.-a=x:Execute permission for all users.! (NOT):Negates the expression.
Example:
find . -type f -perm -u=s -print -exec chmod 644 {} \; # Finds files with the set-user-ID bit set and changes their permissions to 644.
-
-empty:Matches empty files.find . -type f -empty # Finds all empty regular files.
-
-user:Matches files owned by a specific user.find . -type f -user john # Finds files owned by the user "john".
-
-group:Matches files belonging to a specific group.find . -type f -group developers # Finds files belonging to the group "developers".
-
-mtime:Matches files modified within a specified time.find . -type f -mtime +30 # Finds files modified more than 30 days ago.
-
-atime:Matches files accessed within a specified time.
find . -type f -atime +30 # Finds files accessed more than 30 days ago.-amin:Matches files accessed within a specified number of minutes.
find . -type f -amin +60 # Finds files accessed more than 60 minutes ago.-cmin:Matches files changed within a specified number of minutes.
find . -type f -cmin +60 # Finds files changed more than 60 minutes ago.Note
You can combine multiple options to create more specific searches. For example, to find all executable files modified in the last week, you could use:
find . -type f -perm -a=x -mtime -7