Find Command in Unix: Find Files with Unix Find File (Examples)

Introduction to Find Command in Unix: Search files and directories with Unix Find File Command

The Unix find command is a powerful utility to search for files or directories.

The search can be based on different criteria, and the matching files can be run through defined actions. This command recursively descends the file hierarchy for each specified pathname.

=> Click here for Complete Unix Tutorial series

find command in unix

Find Command in Unix

Syntax:

find [options] [paths] [expression]

The options for this command are used to specify how symbolic links should be treated. This is followed by the set of paths to search in. If no paths are specified, then the current directory is used. The given expression is then run on each of the files found in the paths.

The expression consists of a series of options, tests, and actions, each returning a boolean. The expression is evaluated left to right for each file in the path until the result is determined i.e. the result is known to be true or false.

  • Option expressions are used to constrain the find operation, and always return true.
      • -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.
  • Test expressions are used to evaluate specific properties of the files and return true or false accordingly. (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.

Examples

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.

=> Click here for Complete Unix Tutorial series