Languages/shell/find
(Difference between revisions)
Marc.pignat (Talk | contribs) |
Marc.pignat (Talk | contribs) (→Finding files containing a string) |
||
Line 27: | Line 27: | ||
== Finding files containing a string == | == Finding files containing a string == | ||
− | <code>find . -type f -exec grep -q "heivs/error.h" {} \; -print</code><br /> | + | <code>find . -type f -exec grep -q "heivs/error\.h" {} \; -print</code><br /> |
Will find all files containing heivs/error.h | Will find all files containing heivs/error.h | ||
* The <code>-exec</code> command will execute grep on every file found by file, and evaluates it's return value. | * The <code>-exec</code> command will execute grep on every file found by file, and evaluates it's return value. | ||
* The <code>-print</code> command is necessary to display the file that match the <code>-exec</code> command. | * The <code>-print</code> command is necessary to display the file that match the <code>-exec</code> command. | ||
* The <code>{}</code> is the filename given by find to grep | * The <code>{}</code> is the filename given by find to grep | ||
+ | * '''Warning''' grep uses [http://en.wikipedia.org/wiki/Regular_expression regular expressions] for matching, so the dot (.) must be escaped | ||
== Finding files with any of 2 suffixes == | == Finding files with any of 2 suffixes == |
Revision as of 17:37, 3 February 2015
The find
command
This command can be used to find files (or directories) recursively.
Finding all files and directories starting from the current directory
find .
Will display all files and directories below the current directory.
Finding all files and directories in a directory
find /etc
Will display all files and directories below /etc.
Finding all files and directories in many directory
find /etc /home
Will display all files and directories below /etc and /home (in that order).
Finding only files in a directory
find /etc -type f
Will display all files (no directories) below /etc.
Finding files with a suffix
find /etc -type f -name "*.conf"
Will find all files with name ending by .conf below /etc.
- Warning: Don't forget the double quotes (") around the file name
- Warning: find is case sensitive, if you also want files with "*.Conf", use the
-iname
Finding files containing a string
find . -type f -exec grep -q "heivs/error\.h" {} \; -print
Will find all files containing heivs/error.h
- The
-exec
command will execute grep on every file found by file, and evaluates it's return value. - The
-print
command is necessary to display the file that match the-exec
command. - The
{}
is the filename given by find to grep - Warning grep uses regular expressions for matching, so the dot (.) must be escaped
Finding files with any of 2 suffixes
find . -type f -name "*.h" -o -name "*.c"
- The
-o
parameter means OR.
Removing empty directories
find . -type d -empty -exec rmdir {} \;