Skip to content

Find Usage

dimahsu edited this page Aug 7, 2018 · 1 revision
## Basic syntax ##
find /dir/to/search/ -options -name 'regex' -action
find /dir/to/search/ -options -iname 'regex' -action
find /dir/to/search/ -type f -name 'regex' -print
find /dir/to/search/ -type f -name \( expression \) -print
 
## ---------------------------------------------------------------------- ##
## The -and operator is the logical AND operator                          ## 
find /dir/to/search/ -type f -name 'expression -and expression' -print
 
## ---------------------------------------------------------------------- ##
## The -or operator is the logical OR operator.  The expression evaluates ##
## to true if either the first or the second expression is true.          ##
find /dir/to/search/ -type f -name 'expression -or expression' -print


$ find . -type f \( -iname "*.c" -or -iname "*.asm" \)

$ find . -type f \( -name "*.conf" -or -name "*.txt" \) -print

Understanding find command operators

Operators build a complex expression from tests and actions. The operators are, in order of decreasing precedence:

( expr ) Force precedence. True if expr is true
expr -not expr ! expr True if expr is false. In some shells, it is necessary to protect the ‘!’ from shell interpretation by quoting it.
expr1 -and expr2
expr1 -or expr2 expr2 is not evaluated if expr1 is true.

Find *.txt file but ignore hidden .txt file such as .vimrc or .data.txt file:

$ find . -type f \( -iname "*.txt" ! -iname ".*" \)

Find all .dot files but ignore .htaccess file:

$ find . -type f \( -iname ".*" ! -iname ".htaccess" \)

path option

cd $HOME
find . -type f -name "*.txt" ! -path "./Movies/*" ! -path "./Downloads/*" ! -path "./Music/*" 
 
## add -ls option to get ls -l kind of output ##
find . -type f -name "*.txt" ! -path "./Movies/*" ! -path "./Downloads/*" ! -path "./Music/*" -ls

Clone this wiki locally