Languages/shell/find
From UIT
Revision as of 19:02, 3 December 2014 by Marc.pignat (Talk | contribs)
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
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 {} \;