Saturday, January 9, 2016

File & Directory Searches in Linux Command Line using find

Let's learn how to search files & directories in Linux command line. The syntax is very simple:
$ find /location/to/find -name 'name_to_search'

Example 1: find all files and directories with the name etc
$ find / -name 'etc'

Example 2: find files and directories inside /home directory with the name .bashrc
$ find /home -name '.bashrc'


OK. Let's add some complication. Assume that you want to find files and directories that contain some given name. You can do this by:
$ find /location/to/find -name '*name_to_search*'

Example 3: find all files and directories that contain the name etc
$ find / -name '*etc*'

Example 4: find files and directories inside /home directory that contain the name bash
$ find / -name '*bash*'

Here, the * symbol acts as a wildcard that can be any number of any character including none. Since we put the search keyword in between the wildcard *, we can find all instances of files and directories that contain the given keyword regardless of its position.

Another wildcard is ?, which can be any single character. Let's do some examples:

Example 5: find all files and directories whose names are four letters with the last three being ate
$ find / -name '?ate'

Example 6: find all files and directories whose names are four letters with the first three being dum
$ find / -name 'dum?'


We can mix the * and ? wildcards too.

Example 7: find all files and directories whose names end with three letters, of which the first and last are n and x, respectively
$ find / -name '*n?x'


We can add restricted wildcard by using []. Let's go straight to the example.

Example 8: find all files and directories whose names are three letters and start with b, c, or r and end with at
$ find / -name '[bcr]at'

Example 9: find all files and directories inside the current directory whose names are three letters and start with any character but b, c, nor r and end with at
$ find . -name '[!bcr]at'

Example 10: find all files and directories whose names ends with w through z
$ find / -name '*[w-z]'

Example 10: find all files and directories whose names end with anything but alphabet letters or numbers
$ find / -name '*[!a-z,A-Z,0-9]'

The - character indicates all letters or numbers in between, whereas the ! indicates the negation of what is in [].


You must precede with \ when searching for special characters.

Example 11: find all files and directories whose names contain one or more spaces
$ find / -name '*\ *'

Example 12: find all files and directories whose names contain "
$ find / -name '*\"*'

Example 13: find all files and directories whose names contain  \
$ find / -name '*\\*'


Example 14: find all files and directories whose names contain ?
$ find / -name '*\?*'


Example 15: find all files and directories whose names contain *
$ find / -name '*\**'


By the way, you may see a bunch of Permission denied outputs. In this case, run with root privilege
$ sudo find / -name 'name_to_search'
or you can suppress standard error messages by
$ find / -name 'name_to_search' 2> /dev/null

Lastly, one can use -iname instead of -name to search with the case-insensitive option.

1 comment: