Linux - find command

About

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

Find use stat to extract its information

File Status = File Metadata

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’).

Filtering

Tree Level

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

This section shows the filter based directly on the date metadata

Time
Argument Type Description
Access time filter (axxx options)
-amin n Test File was last accessed n minutes ago. See File’s status
-anewer file Test File was last accessed more recently than file was modified. See File’s status
-atime n Test File was last accessed n*24 hours ago. See File’s status
Changed time filter (cxxx options)
-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.
Modify time filter (mxxx options)
-mmin n Test File's data was last modified n minutes ago.
-mtime n File's data was last modified n*24 hours ago.

Name

File name
Argument Type Description
-name pattern Test Base of file name

Example

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

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.

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.

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

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/ {} \+

Documentation / Reference


Powered by ComboStrap