Linux find command is one of the most powerful tools in the Linux system administrators’ arsenal.
The basic syntax for the find command in Linux is:
find [path] [expression]
where path is the starting directory for the find command, and expression is a set of conditions that must be satisfied for a file to be considered a match.
Get Your Free Linux training!
Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!Here are some common options that can be used with the find command:
-name pattern: Matches files and directories whose names match the specified pattern.
-type type: Matches files of the specified type, where type can be f for regular files, d for directories, l for symbolic links, and others.
-size [+|-]size: Matches files whose size is greater than (+) or less than (-) the specified size. The size can be specified in bytes, kilobytes (k), megabytes (M), gigabytes (G), or other units.
-mtime [+|-]days: Matches files whose modification time is greater than (+) or less than (-) the specified number of days.
-exec command {} \;: Executes the specified command on each file that matches the expression. The {} is a placeholder for the file name that find found, and the \; signals the end of the -exec command.
There are many other options available for the find command, and they can be combined in various ways to perform complex searches and operations on files and directories.
Find all files in the current directory modified more than 7 days ago:
find . -type f -mtime +7
Find all directories in the current directory with names starting with “data”:
find . -type d -name "data*"
Find all files in the current directory with names matching “report*.pdf” or “presentation*.pdf”:
find . -type f \( -name "report*.pdf" -o -name "presentation*.pdf" \)
Find all files in the current directory with a size greater than 1MB:
find . -type f -size +1M
Find all files in the current directory that are owned by the user “john” and group “users”:
find . -type f -user john -group users
Option | Description |
---|---|
-name | Search for files with the specified name |
-type | Search for files of a specific type (directory, file, symbolic link, etc.) |
-size | Search for files with a specific size |
-mtime | Search for files modified within a specific time period |
-exec | Execute a command on each file found |
-delete | Delete each file found |
Print the file path of each file found | |
-empty | Search for empty files or directories |
-perm | Search for files with specific permissions |
-user | Search for files owned by a specific user |
-group | Search for files owned by a specific group |
-maxdepth | Set the maximum depth of directories to search |
-mindepth | Set the minimum depth of directories to search |
-regex | Search for files matching a regular expression |
-iname | Search for files with a specified name ignoring case |
-not | Invert the next expression |
-and | Combine expressions with a logical “and” |
-or | Combine expressions with a logical “or” |