find find is a tool which looks for files on a filesystem. find has a large number of options which can be used to customise the search (refer to the manual/info pages).
Note that find works with standard wildcards, Section 20.4.1, and can work with regular expressions, Section 20.4.2.
Basic example:
This would look for a file named "file" and start at the root directory (it will search all directories including those that are mounted filesystems).
The `-name' option is case sensitive you can use the `-iname' option to find something regardless of case.
Use the '-regex' and '-iregex' to find something according to a regular expression (either case sensitive or case insensitive respectively).
The '-exec' option is one of the more advanced find operations. It executes a command on the files it finds (such as moving or removing it or anything else...).
To use the -exec option: use find to find something, then add the -exec option to the end, then:
command_to_be_executed then '{}' (curly brackets) then the arguments (for example a new directory) and finally a ';' .
|
See below for an example of use this command.
- This is the tool you want to execute on the files find locates. For example if you wanted to remove everything it finds then you would use -exec rm -f
- The curly brackets are used in find to represent the current file which has been found. ie. If it found the file shopping.doc then {} would be substituted with shopping.doc. It would then
continue to substitute {} for each file it finds. The brackets are normally protected by backslashes (\) or single-quotation marks ('), to stop bash expanding them (trying to interpret them as a
special command eg. a wildcard).
- This is the symbol used by find to signal the end of the commands. It's usually protected by a backslash (\) or quotes to stop bash from trying to expand it.
find / -name '*.doc' -exec cp '{}' /tmp/ ';'
|
The above command would find any files with the extension '.doc' and copy them to your /tmp directory, obviously this command is quite useless, it's just an example of what find can do. Note that
the quotation marks are there to stop bash from trying to interpret the other characters as something.
Excluding particular folders with find can be quite confusing, but it may be necessary if you want to search your main disk (without searching every mounted filesystem). Use the
-path option to exclude the particular folder (note cannot have a '/' (forward slash) on the end) and the -prune option to exclude the subdirectories. An example is below:
find / -path '/mnt/win_c' -prune -o -name "string" -print
|
This example will search your entire directory tree (everything that is mounted under it) excluding /mnt/win_c and all of the subdirectories under /mnt/win_c. When using the -path option
you can use wildcards.
Note that you could add more -path '/directory' statements on if you wanted.
Find has many, many different options, refer to the manual (and info) page for more details.