Find: Difference between revisions
mNo edit summary |
find by owner, moved operators up. |
||
| Line 1: | Line 1: | ||
{{code|find}} is one of those commands that can be quite arcane. Hopefully this article should clear things up with a brief list of basic uses. | |||
== Find | == Find Expressions == | ||
== By File Type == | |||
Basic usage: | Basic usage: | ||
find . '''-type''' [f - file|d - directory|l - links] | find . '''-type''' [f - file|d - directory|l - links] | ||
| Line 11: | Line 13: | ||
find . -type f -o -type d | find . -type f -o -type d | ||
== | == By filename == | ||
Basic usage: | Basic usage: | ||
find . -['''i''' - case insensitive]name <term> | find . -['''i''' - case insensitive]name <term> | ||
| Line 28: | Line 30: | ||
find . -regextype posix-egrep -regex ".?\/?[0-9]{3}-[0-9]{4}.txt" | find . -regextype posix-egrep -regex ".?\/?[0-9]{3}-[0-9]{4}.txt" | ||
== | == By time (created, access, modified) == | ||
Basic usage: | Basic usage: | ||
find . -['''a''' - accessed|'''c''' - created|'''m''' - modified]['''time'''|'''min'''] <time> | find . -['''a''' - accessed|'''c''' - created|'''m''' - modified]['''time'''|'''min'''] <time> | ||
| Line 40: | Line 42: | ||
find . -cmin -1 | find . -cmin -1 | ||
== | == By Ownership (groups or user) == | ||
Basic usage: | Basic usage: | ||
find . | find . -[user|group] <name or id> | ||
Find is able to find file owners by their username or user ID or by group name or group ID. | |||
To find all files owned by leo: | |||
find . -user leo | |||
find . - | |||
== Operators == | == Operators == | ||
| Line 94: | Line 81: | ||
| Can also use -or | | Can also use -or | ||
|} | |} | ||
== Do things on match == | |||
Basic usage: | |||
find . '''-exec''' <command> {} \; | |||
The {} is substituted with the matching filename. | |||
The \; at the end denotes the end of the command passed to -exec. | |||
=== Move all .o files to /tmp/objects === | |||
find . -iname "*.o" -exec mv {} /tmp/{} \; | |||
<code>/tmp/{}</code> can be simplified to </code>/tmp</code> as in <code>-exec mv {} /tmp</code> - but the example above shows that you can use {} more than once. | |||
=== Convert all .php files into UTF-8 files === | |||
The following are two separate commands. Run them sequentially. | |||
find . -iname "*.php" -exec sh -c 'iconv -f cp1252 -t utf-8 {} > {}.utf8' \; | |||
for i in `(find . -name "*.utf8")`; do mv $i ${i/.utf8/}; done | |||
=== Change the file mode for files to 644 and directories to 755 === | |||
find . -type f -exec chmod -v 644 {} \; | |||
find . -type d -exec chmod -v 755 {} \; | |||
=== Remove all broken symlinks === | |||
find . -type l -! -exec test -e {} \; -print | while read i ; do rm -v $i ; done | |||
Revision as of 18:19, 2 December 2015
find is one of those commands that can be quite arcane. Hopefully this article should clear things up with a brief list of basic uses.
Find Expressions
By File Type
Basic usage:
find . -type [f - file|d - directory|l - links]
To find only normal files:
find . -type f
To find both files and directories:
find . -type f -o -type d
By filename
Basic usage:
find . -[i - case insensitive]name <term>
The term you pass in must match the entire file name. You can use * as a wild card.
For regex matching, use -regex. The default regex used by find is emacs-regex. To define something else (like posix), use -regextype.
To find a file named exactly 'confession.txt':
find . -name confession.txt -type f
To find a directory named 'backup' or 'Backup':
find . -iname backup -type d
To find a file with the format NNN-NNNN.txt where N is an integer:
find . -regextype posix-egrep -regex ".?\/?[0-9]{3}-[0-9]{4}.txt"
By time (created, access, modified)
Basic usage:
find . -[a - accessed|c - created|m - modified][time|min]
where time is in days when used with time, or minutes when used with min.
To find files modified within 3 minutes ago:
find . -mmin -3
To find files created within 1 day ago:
find . -cmin -1
By Ownership (groups or user)
Basic usage:
find . -[user|group] <name or id>
Find is able to find file owners by their username or user ID or by group name or group ID.
To find all files owned by leo:
find . -user leo
Operators
You can mix and match the expressions discussed above with operators.
| Operator | Description | Example | Notes |
|---|---|---|---|
| ( ) | Force precedence | ( -type f -o type d ) -a -name "exception.txt" | |
| ! | not | ! -name "exception.txt" | Can also use -not |
| -a | And | -type f -a -name "confession.txt" | This is the default operator. Can also use -and |
| -o | Or | -name "confession.txt" -o -name "murder.txt" | Can also use -or |
Do things on match
Basic usage:
find . -exec <command> {} \;
The {} is substituted with the matching filename. The \; at the end denotes the end of the command passed to -exec.
Move all .o files to /tmp/objects
find . -iname "*.o" -exec mv {} /tmp/{} \;
/tmp/{} can be simplified to /tmp as in -exec mv {} /tmp - but the example above shows that you can use {} more than once.
Convert all .php files into UTF-8 files
The following are two separate commands. Run them sequentially.
find . -iname "*.php" -exec sh -c 'iconv -f cp1252 -t utf-8 {} > {}.utf8' \;
for i in `(find . -name "*.utf8")`; do mv $i ${i/.utf8/}; done
Change the file mode for files to 644 and directories to 755
find . -type f -exec chmod -v 644 {} \;
find . -type d -exec chmod -v 755 {} \;
Remove all broken symlinks
find . -type l -! -exec test -e {} \; -print | while read i ; do rm -v $i ; done