Linux - find command

> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU) > Bash - File System

1 - About

Search, print information and takes actions on files in a directory hierarchy.

Find use stat to extract its information

File Status = File Metadata

Advertising

3 - Synopsis/Syntax

find [-H] [-L] [-P] [path...] [options] [^(-|(|)|,|!)argument....]
Synbol Default Description
'-H', '-L' and '-P' options control the treatment of symbolic links
path current directory names of files or directories to be examined
^(-|(|)|,|!)argument.... -print
(consider -print0)
expression describing what is to be searched for.
The argument expressions begins with ‘-’, ‘(’, ‘)’, ‘,’, or ‘!’

The expression is made up of:

  • options (which affect overall operation rather than the processing of a specific file, and always return true),
  • tests (which return a true or false value),
  • actions (which have side effects and return a true or false value), all separated by operators.
  • operators. -and is assumed where the operator is omitted. The POSIX standard specifies parentheses ‘(’, ‘)’, negation ‘!’ and the ‘and’ and ‘or’ operators (‘-a’, ‘-o’).

3.1 - Filtering

Argument Type Description
Level
-maxdepth levels Option Descend at most levels (a non-negative integer) levels of directories below the command line arguments.
'-maxdepth 0' means only apply the tests and actions to the command line arguments.
-mindepth levels Option Do not apply any tests or actions at levels less than levels (a non-negative integer).
‘-mindepth 1’ means process all files except the command line arguments.
Time
-amin n Test File was last accessed n minutes ago.
-anewer file Test File was last accessed more recently than file was modified.
-atime n Test File was last accessed n*24 hours ago.
-cmin n Test File’s status was last changed n minutes ago.
-cnewer file Test File’s status was last changed more recently than file was modified.
-ctime n Test File’s status was last changed n*24 hours ago.
File name
-name pattern Test Base of file name
Advertising

4 - Example

4.1 - Search from the current directory

find . -name 'pattern'

where:

  • the point specify to search from the current directory (in or below)
  • the pattern is a regexp pattern of the file name

4.2 - Search by file name

Search for any file named root.sh in or below the directory /my/directory.

find /my/directory -name root.sh

Search for any file beginning with default

find /my/directory -name default*

The metacharacters (‘*’, ‘?’, and ‘[]’) match a ‘.’ at the start of the base name.

4.3 - Listing recursively file information

find . -printf "%p %u %g %M %a %c\n"
# for sizing
echo "Relative_Path,Depth,Leading_Dir,Size_in_Byte,User_Name,Last_access_time,Last_change_time" > diskInfo.csv
find . -printf '"%p","%d","%h","%s","%u","%AY-%Am-%Ad","%CY-%Cm-%Cd"\n' >> diskInfo.csv

where:

Path:

  • %p is the relative path (File’s name)
  • %d File’s depth in the directory tree; 0 means the file is a command line argument.
  • %h Leading directories of file’s name (all but the last element).

Size:

  • %k The amount of disk space used for this file in 1K blocks
  • %s File’s size in bytes.

User/Security

  • %u is the user. File’s user name, or numeric user ID if the user has no name
  • %g is the group
  • %M and %m are permissions (respectively in symbolic form as ls and octal form)

Time:

  • %a is the File’s last access time. See %Ak to specify a date format. Example: %AY-%Am-%Ad - YYYY-MM-DD
  • %c is the File’s last status change time. See %Ck to specify a date format
  • %t File’s last modification time in the format returned by the C ‘ctime’ function.
Advertising

4.4 - Search newer file

-newer file: File was modified more recently than file. If file is a symbolic link and the -H option or the -L option is in effect, the modification time of the file it points to is always used.

touch -d '2011-12-31 10:22' foo
find . -newer foo

4.5 - Action on file with exec

Syntax:

find -exec command {} \;
find -exec command {} \+

where:

  • \ in \; is an escape sequence. ; is the end of the exec command not of the find command.
  • {} is a placeholder for the filename found.
  • + will make only one command invocation

Example:

  • Remove the directory named “jsp_servlet”
 find . -name "jsp_servlet" -exec rmdir {} \;
  • Moving all library file
find . -type f -iname '*.so' -exec mv -t ./test/ {} \+

5 - Documentation / Reference