Find is an extremely useful command for finding files and directories. It searches based a specified condition at a specified directory and descends into all subdirectories of the specified directory. You must always specify a directory and a condition.
You can specify the following:
* path (where to search)
* file type (-type: directories, data files, links)
* how to process the files (-exec: run a process against a selected file)
* file name (-name: partial or full name of file(s))
* logical operations on selections (-o and -a)
Find offers lots of very powerful options which allow you to precisely define your search criteria. Following are the most useful ones:
Find files and printing their path
find . -name "httpd.conf" -print
This command will search in the current directory and all it sub directories for httpd.conf file. The -print option would print out the paths of matching results.
List all files named myfile in you home directory
find $HOME -name myfile -print
List all files beginning with ca in the project directory
find /project -type f -name ´ca*´ -print
-type f indicates that we are looking for a file
Seach the filesystem for manpage directories
find / -type d -name ´man´ -print
-type d indicates that we are looking for a directory
Remove all empty files
find / -size 0 -ok rm \;
-size indicates the file size.
Find all files modified within the last 90 days
find / -mtime -90 -print
Finding files and running commands on them
find . -name "httpd.conf" -exec chmod 755 '{}' \;
This command would search for httpd.conf in the current directory and its sub directories. chmod 755 command would be applied to all matching files. The argument '{}' inserts each matching file into command line. The\; argument indicates end of exec command line.
Apply logical operations on selections
find /var/www/html/mysite -not \( -name "*.php" -o -name "*.inc" \) '{}' \; -print
This command will search the directory /var/www/html/mysite. All files ending in .php or .inc would be excluded.
-name "*.php" -o -name "*.inc"
This command means search for all files with name ending in .php and .inc
-not \( -name "*.php" -o -name "*.inc" \)
-not negates the argument. Thus it searches for all files not ending in .php and inc. \( and \) define the boundary of -not. -o means a logical expression. Here -o refers to or.
Finding files containing a string
So far we had limited ourselves to searching by file name. We can also search for files containing certain strings of characters. This is accomplished using the grep command.
find . -exec grep "edit-this-directory" '{}' \; -print
This command will search the current directory and its sub directories to return all files that contain the string "edit-this-directory".
Finding files containing a string but ignoring svn files
If you are using subversion, you would be annoyed that find is looking through svn-specfic files and returning results from it. To exclude svn files, use
find . -not \( -name *.svn-base \) -exec grep "edit-this-directory" '{}' \; -print
This command will search the current directory and its sub directories to return all files not ending in svn-base which contain the string "edit-this-directory".
Finding executable files
To find executable files, you should use which or whereis commands. Some linux versions offer GNU locate to find files. All versions of unix and Linux fully support the find command.