# Guide to Unix/Commands/Finding Files

| Wikibooks Guide to Unix Computing | Edit template |
   General: Introduction | Explanations | Why Unix-like Platforms: Linux | BSD Quick Reference: Commands | Environment Variables | Files | License
   Commands: Getting Help | File System Utilities | Finding Files | Devices | File Viewing | File Editing | Text Processing | File Compression | File Analysing | Multiuser Commands | Self Information | System Information | Process Management | Kernel Commands | Miscellaneous | SW Development |

### find

Searches a given path for a file or folder. The syntax is: find [path...] [expression...]
Examples: On some of the latest Unix-like OS's, the -print option is a default and can be omitted. The following command searches for the file 'grub.conf' starting at the root ('/') directory.
If you are not the administrator of the computer, you get error messages for all the directories you are not allowed to read. In this case do it like this for a bash shell:
Or like this for a csh/tcsh:
If you want to ignore the case of the characters, you can use -iname. The following will find files with extensions txt, TXT, Txt, and so on:
The following command will search for all directories named 'local'.
The above example combined two tests - filename (-name) and filetype (-type) - and returned files satisfying both (logical AND).
It's also possible to specify two or more tests, and return files satisfying any of them, with the -o (logical OR) directive. The tests to be ORed must be grouped together between (...) (which must be escaped with \ as parentheses are special to the shell). The following return files with the extensions txt or doc, as well as any file larger than 5MB (megabyte) in size.
Where the last file satisfied two tests.
Note: Each directive must be complete, tests cannot be omitted. E.g. -name '*.txt' -o '*.doc' (omitting the 2nd -name) is not valid.
Tips: Using 'exec' option executes certain commands for each file found by find:
Using 'ok' has same effect but it will prompt for every file:
Warning: When using "-exec" or "-ok", a semicolon must be used to indicate the end of the arguments (to "rm" in the example). Because semicolon is special to the shell, it must be quoted. The above examples quote the semicolon with a backslash.
Links:
* find, opengroup.org
* find man page, man.cat-v.org
* Finding Files, gnu.org

### whereis

Searches the normal executable and man page locations for a specified file. Does not seem covered by POSIX.
Examples:
Links:
* whereis man page, man7.org
* whereis man page, freebsd.org

### which

Searches the locations in your PATH variable for a specified file. If you know a program is in your path (i.e. you can run it) this is faster than whereis.
Links:
* GNU which project, savannah.gnu.org
* which man page, freebsd.org

### locate

Finds all filenames that match the specified query. Absent from traditional UNIX, is GNU software and comes standard with Linux.
Examples:
Note that locate uses a database of already collected filenames, that is typically updated once every 24 hours. As such, using locate will not correctly show newly created or deleted files/directories.
Links:
* 8.2 Invoking locate in GNU Findutils manual, gnu.org
* locate man page, freebsd.org

### xargs

Turns items in its input stream into arguments of another command to invoke. One common use is in conjunction with find.
Examples:
* echo a b c | xargs -n1 echo
** Outputs a, b, and c, each one on a separate line; -n1 specifies that at most 1 input item should be passed to the command at a time.
* echo a,b,c | xargs -d, -n1 echo
** In certain versions of GNU xargs, outputs a, b, and c, each one on a separate line. -d specifies the item delimiter.
Links:
* xargs, opengroup.org
* xargs, freebsd.org
* 8.4 Invoking xargs in GNU Findutils manual, gnu.org
* xargs source code, git.savannah.gnu.org
* W:xargs