0% found this document useful (0 votes)
3 views

FIND Commands

The document provides a comprehensive overview of the 'find' command in Unix, detailing its syntax, options, and expressions for searching files and directories. It includes various flags for filtering results based on file attributes, as well as action expressions for manipulating matched files. Additionally, it offers practical examples of using the 'find' command for different search scenarios.

Uploaded by

abhik.das84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

FIND Commands

The document provides a comprehensive overview of the 'find' command in Unix, detailing its syntax, options, and expressions for searching files and directories. It includes various flags for filtering results based on file attributes, as well as action expressions for manipulating matched files. Additionally, it offers practical examples of using the 'find' command for different search scenarios.

Uploaded by

abhik.das84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

FIND Commands

find [options] [paths] [expression]

 -depth: process the directory contents before processing the


directory itself.
 -maxdepth: the max levels below the provided paths to descend
for a match.
 -mindepth: the min levels beyond the provided paths to descend
before matching.

Wherever a count ‘n’ is used: without any prefix the match is for the exact
value of n; with a ‘+’ prefix, the match is for values greater than n; and
with a ‘-‘ prefix, the match is for values lesser than n.

 -atime n: Returns true if the file was accessed n days ago.


 -ctime n: Returns true if the file’s status was changed n days ago.
 -mtime n: Returns true if the file’s contents were modified n days
ago.
 -name pattern: Returns true if the file’s name matches the provided
shell pattern.
 -iname pattern: Returns true if the file’s name matches the provided
shell pattern. The matching here is case insensitive.
 -path pattern: Returns true if the file’s name with the path matches
the shell pattern.
 -regex pattern: Returns true if the file’s name with the path matches
the regular expression.
 -size n: Returns true if the file size is n blocks.
 -perm – mode: Returns true if all the permission bits for mode are
set for the file.
 -type c: Returns true if the file is of type c (e.g. ‘b’ for block device
file, ‘d’ for directory etc.).
 -username: Returns true if the file is owned by username ‘name’.

The action expressions are used to define actions that have side effects
and may return true or false. If not actions are specified, the ‘-print’ action
is performed for all matching files.

 -delete: Delete the matched file, and return true if successful.


 -exec command: Execute the given command for each matching
file, and return true if the return value is 0.
 -ok command: Like the ‘exec’ expression, but confirms with the
user first.
 -ls: List the matching file as the per ‘ls -dils’ format.
 -print: Print the name of the matching file.
 -prune: If the file is a directory, do not descend into it, and return
true.
The expression is evaluated from left to right and is put together using the
following operators.

 \( expr \): Used to force precedence.


 ! expr: Used to negate an expression.
 expr1 -a expr2: The result is an ‘and’ of the two expressions. The
expr2 is only evaluated of expr1 is true.
 expr1 expr2: The ‘and’ operator is implicit in this case.
 expr1 -o expr2: The result is an ‘or’ of the two expressions. The
expr2 is only evaluated of expr1 is false.

List all files found in the current directory and its hierarchy
$ find.

List all files found in the current hierarchy, and all the hierarchy
below /home/xyz
$ find. /home/XYZ

Search for a file by the name abc in the current directory and its
hierarchy
$ find ./ -name abc

Search for a directory by the name xyz in the current directory


and its hierarchy
$ find ./ -type d -name xyz

Search for a file by the name abc.txt below the current directory,
and prompt the user to delete each match.
Note that the “{}” string is substituted by the actual file name while
running and that the “\;” string is used to terminate the command to be
executed.

$ find ./ -name abc.txt -exec rm -i {} \;

Search for files that were modified in the last 7 days below the
current directory
$ find ./ -mtime -7

Search for files that have all permissions set in the current
hierarchy
$ find ./ -perm 777

Conclusion
In short, Find Command in Unix returns all files below the current working
directory. Further, find command allows the user to specify an action to be
taken on each matched file.

You might also like