Table of Contents

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:

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:

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*'

Search for any yaml or yml file

find /my/directory \( -name "*.yaml" -o -name '*.yml' \)

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:

Size:

User/Security

Time:

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

Loop over file

while IFS= read -r -d '' file; do

   echo "Yaml File: $file"

done < <(find . -type f \( -name "*.yaml" -o -name '*.yml' \) -print0)

Action on file with exec

Syntax:

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

where:

A -exec command must be terminated with a ;. A parameter of its own, not a shell separator. You usually need to type \; or ';' to avoid shell interpretation.

Example:

 find . -name "jsp_servlet" -exec rmdir {} \;
find . -type f -iname '*.so' -exec mv -t ./test/ {} \+

Documentation / Reference