find options startingPoint expression
startingPoint :Code | Condition | Details |
---|---|---|
0 | all files were processed successfully. This includes occurrences where find found no match. | find ~ -name aFileWithAVeryUncommonName; echo $?0 |
!=0 | an error occurred | find ~ -name; echo $?find: missing argument to `-name' 1 |
Flag | Usage |
---|---|
-daystart | For -amin, -atime, -cmin, -ctime, -mmin and -mtime :
Albeit causing no error, this flag makes no sense with the *min flags (details).
|
-H | Do not follow symlinks, except while processing the command line arguments |
-L | dereference symlinks |
-maxdepth n | target is at most n subdirectories deep. To investigate the current directory only (i.e. no recursion) : find -maxdepth 1 |
-mindepth n |
|
-mount | Don't descend directories on other filesystems |
-P | Never follow symlinks (default behavior) |
-regextype | specify the RegExp ruleset to use. By default, find uses the GNU Emacs RegExp ruleset. |
Flag | Usage |
---|---|
-amin n | target was last accessed n minutes ago (Details on Numeric Arguments, details on atime / ctime / mtime) Before using this, make sure the considered filesystem doesn't use the noatime mount option.
|
-atime n | target was last accessed n*24 hours ago (Details on Numeric Arguments, details on atime / ctime / mtime) Before using this, make sure the considered filesystem doesn't use the noatime mount option.
|
-cmin n | target's status was last changed n minutes ago (Details on Numeric Arguments, details on atime / ctime / mtime) |
-ctime n | target's status was last changed n*24 hours ago (Details on Numeric Arguments, details on atime / ctime / mtime) |
-empty | find empty objects :
|
-group groupName | search files owned by the group groupName |
-inum n | search files having the inode number n -samefile does the same. |
-mmin n | target was last modified n minutes ago (Details on Numeric Arguments, details on atime / ctime / mtime) |
-mtime n | target was last modified n*24 hours ago (Details on Numeric Arguments, details on atime / ctime / mtime) |
-name 'pattern' -iname 'pattern' |
target file name matches pattern (such patterns are called globular expressions (i.e. wildcards)) same as above for case-insensitive searches. (Details on "Pattern matching" vs "filename expansion") In order to protect it against shell expansion, pattern must be quoted when including wildcards. Single quotes seem to do the job in any case.
|
-path pattern -ipath pattern |
|
|
|
-regex -iregex | perform a RegExp / case insensitive RegExp search (examples) |
-samefile someFile | search files sharing the same inode as someFile (i.e. hard links) |
-size expression | expression is [prefix][number][unit] :
When looking for 0 byte files, consider empty.
examples :
|
-type t | search files of type t (list of file types) :
|
-user bob | search files owned by Bob |
-xtype c | find "scans" files (everything is a file, remember ) and filters them to display those matching :
|
Operators | Usage |
---|---|
-a | logical AND : expr1 -a expr2
|
-o | logical OR : expr1 -o expr2 expr2 is not evaluated if expr1 is true |
! expr | negates expr
This can be used to exclude file names from the results without using grep -v :
find -type f ! -name '*l'
|
-not expr | same as ! expr, but not POSIX compliant |
( )
:\
[SPACE]
-name "*1*" -o -name "*2*"
\) -name "*b"; rm "$prefix"*; cd - && rmdir "$workDir"file_1.a all the test files we just created file_1.b file_1.c file_2.a file_2.b file_2.c file_3.a file_3.b file_3.c ./file_1.b those having'1' OR ('2' AND 'b')
./file_1.a ./file_2.b ./file_1.c ./file_1.b those having('1' OR '2') AND 'b'
./file_2.b
Flag | Usage |
---|---|
-delete | someway similar to -exec rm {} +. -delete conflicts with -prune : |
-exec command |
Looks like the -exec part is executed only if the preceding condition is "true" :
If find [options] -ls displays much more lines than find [options], it _may_ be because one of the results is a directory .
|
-ls | short for -exec ls -dils {} + |
-ok command | similar to -exec command, but prompts before executing command.
|
-prune | ignore a whole directory tree. This is done by specifying this directory prior to the -prune directive itself (details) :
find -path "directoryToIgnore" -prune
|
When finding by file name with :
/
{n}
( ) |
[ ]
/path/to/storage ├── dir1 │ ├── ... │ │ ├── ... │ │ └── ... │ ├── dir2 │ ├── ... │ │ ├── ... │ │ └── ... ├── ... │ ├── ... ├── dir3 │ ├── ... │ │ ├── ... │ │ └── ... │ ├── ... └── ...
If the search region (here .) is specified as a relative path, the path to exclude from the search (here ./dir_2) must be specified as a relative path too. Same goes on with absolute paths : mixing relative and absolute fails.
The prune expression must be considered as a block : -path excludedDir -prune -o. The -o (logical "OR") seems counter intuitive since we are searching for files that are not in excludedDir AND that match the other search criteria. But actually, it works if you consider the way find evaluates -o and -a operators.
{}
will be replaced by the name of the current found file before running mv;
. To avoid this ;
to be misinterpreted by the shell, it's better to escape it : \;
$
.'
... .*folder\.jpe?g
" -exec bash -c 'newName=$(dirname "{}")/cover.jpg; mv "{}" "$newName"
' +myFunction() { doSomething $1 } # since 'find' starts a new shell, the function requires to be exported to be available. export -f myFunction find . -name script.pl -print -exec bash -c "myFunction {}" +