This command finds files matching a test expression under a particular directory and sub-directories under it.
The basic format of the command needs to be understood. There are four broad sections-- an optional set of arguments governing the general behaviour of the command; the directory hierarchy under which the search operation is to be performed; the test expression against which the files are to be matched; and the action to be carried out on every file that satisfies the specified test expression. In short this is how it should look.
user@box ~$ find [options]
One of the most common things to do is to list all files whose names match a particular regular expression. The '-name' test is used to match the names of the files, and the '-type' option is used to specify the kind of files we are looking for. In this case we want regular files only. For every successful match we use the '-print' action to display the file's name on the standard output. Let us try to find all file names which end with 'rc' under the user's home directory.
user@box ~$ find ~ -name "*rc" -type f -print
Another common task is to find all files that are owned by a particular user. We use the '-user' test to check the owner of each file. So let us have a look at all the files (including directories, symbolic links, etc.) owned by the 'root' user on the system.
user@box ~$ find / -user root -print
One can use the '-perm' test to find files matching a particular set of permissions, and the '-empty' test to look for empty regular files or directories.
user@box ~$ find / -perm 644 -print user@box ~$ find / -empty -print
Till now we have only been using the '-print' action to print out the names of the successful matches. There are other actions too.
System administrators often want to delete all files belonging to a particular user once that user's account has been deleted. If the user had kept files in locations other than his home directory it can be a pain to delete these files manually. You can use the '-delete' action to do this using 'find'. Lets assume that we want to delete all files owned by the user 'foo' on the system. You may need to run this command as root if you do not have permission to delete files owned by 'foo'.
user@box ~$ find / -user foo -delete
We have looked at the most common options that a novice needs to learn to get kickstarted with the 'find' command. However 'find' is a very powerful command with extensive functionality. You can see the manual page for the various and try out the examples given towards the end.
user@box $~ info find user@box $~ man find