Linux Basic - Find Command
Linux Basic - Find Command
The find command in Unix/Linux systems is used to search for files and directories
within a directory hierarchy. It is a powerful tool that offers a wide range of options
for customizing searches based on various criteria. Below is a detailed explanation
of the provided usage and important options:
Usage:
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]
[-H], [-L], [-P]: These options control how symbolic links are followed.
-H: Follow symbolic links specified on the command line.
-L: Follow all symbolic links.
-P: Never follow symbolic links. This is the default behavior.
[-D debugopts]: Debugging options for troubleshooting.
[-Olevel]: Optimization level for expression evaluation.
[starting-point...]: Optional starting points for the search. If not specified,
the search starts from the current directory.
[expression]: Specifies the criteria for selecting files. This can include
various tests and actions.
Important options:
-maxdepth n: Limits the depth of the search to 'n' levels below the starting-
point.
-mindepth n: Starts searching at depth 'n' in the directory hierarchy.
-amin n: File was accessed n minutes ago.
-anewer file: File was last accessed more recently than 'file' was modified.
-atime n: File was last accessed n*24 hours ago.
-cmin n: File's status was last changed n minutes ago.
-cnewer file: File's status was last changed more recently than 'file' was
modified.
-ctime n: File's status was last changed n*24 hours ago.
-min n: File's data was last modified n minutes ago.
-mnewer file: File's data was last modified more recently than 'file' was
modified.
-mtime n: File's data was last modified n*24 hours ago.
-name pattern: Matches filenames using shell-style wildcards.
-iname pattern: Like -name, but case insensitive.
-regex pattern: Matches filenames using regular expressions.
-iregex pattern: Like -regex, but case insensitive.
-type type: Specifies the type of file to search for (e.g., f for regular files, d
for directories, l for symbolic links).
-user username: Searches for files owned by a specific user.
-exec command {} +: Executes 'command' on the selected files.
-a: Logical AND operator for combining multiple expressions.
-o: Logical OR operator for combining multiple expressions.
These options allow for precise control over the search criteria, enabling users to
locate files and directories based on various attributes such as name, type,
ownership, timestamps, and more. The -exec option is particularly useful for
performing actions on the files found during the search.
Some examples:
1. -maxdepth n:
Limits the depth of the search to 'n' levels below the starting-point.
Example: To search for files only in the current directory (depth 1)
find . -maxdepth 1 -type f
2. -mindepth n:
Starts searching at depth 'n' in the directory hierarchy.
Example: To search for files starting from the second level of the
directory hierarchy
find . -mindepth 2 -type f
3. -amin n, -anewer file, -atime n:
These options deal with file access time.
Example: To find files accessed in the last 10 minutes
find . -amin -10
4. -cmin n, -cnewer file, -ctime n:
These options deal with file status change time.
Example: To find files whose status changed in the last 2 hours
find . -cmin -1
20
5. -min n, -mnewer file, -mtime n:
These options deal with file modification time.
Example: To find files modified in the last 24 hours
find . -mtime -1
6. -name pattern, -iname pattern:
Matches filenames using shell-style wildcards (case sensitive or case
insensitive).
Example: To find files with names ending in ".txt"
find . -type f -name "*.txt"
7. -regex pattern, -iregex pattern:
Matches filenames using regular expressions (case sensitive or case
insensitive).
Example: To find files with names starting with "file" or "File"
find . -type f -iregex '.*\/[fF]ile.*'
8. -type type:
Specifies the type of file to search for (e.g., f for regular files, d for
directories, l for symbolic links).
Example: To find directories
find . -type d
9. -user username:
Searches for files owned by a specific user.
Example: To find files owned by the user "john"
find . -user john
10.-exec command {} +:
Executes 'command' on the selected files.
Example: To find and delete all .tmp files
find . -type f -name "*.tmp" -exec rm {} +
11.-a, -o:
Logical AND (-a) and Logical OR (-o) operators for combining multiple
expressions.
Example: To find files modified in the last 7 days and owned by user
"john":
find . -mtime -7 -a -user john
These examples showcase how the find command can be used with various options
to search for files and directories based on different criteria.