Using Find to Search Better

Basic Use of FIND

If you are looking to find a file, one of the most common tools is Find. Here is a recap.

OFILE TYPEDESCRIPTION
1type -fLimits search results to files only
2type -dLimits search results to directories only
3type -lLimits search results to symbolic links only

For example, search for a case-insensitive file named “hello.mov”

$ find $HOME -type -iname "Hello.mov"

Parameters

NOPARAMETERSDESCRIPTION
1-namePerform a case-sensitive search for “files”
2-inamePerform a case-insensitive search for “files”
3size +nMatches files of size larger than size n
4size -nMatches files of size smaller than size n
5-mtime nMatches files or directories whose contents were last modified n*24 hours ago
6-atime nMatches files last access n*24 hours ago

For example, search for all case-insensitive files with the extension *mov 2 days ago

$ find $HOME -type -iname "*.mov" -mtime 2

Operators

S/NOOPERATOREXPLANATION
1-andMatch for both sides of the operators
2-orMatch for either test of the operators
3-noteDon’t match the test of the operators

For example, search for all files with Hello*, but excl ude pdf and jpg

$ find \( -name "Hello*" -mtime 2 \) -and -not \( -iname "*.jpg" -or -iname "*.pdf" \)

When using the () to combine tests, remember to escape the (\) brackets. You will need to leave a space after you open and close the brackets

find -type f -iname "*.mov" -exec chmod +x {} \;

The first part find -type f -iname”*.mov” will not be explained….. Executed commands must end with \; (a backslash and semi-colon) and may use {} (curly braces) as a placeholder for each file that the find command locates.

References:

  1. Linux Format – March Edition
  2. Use the Unix find command to search for files