Getting started with the find utility

inspect-code

This is the first of several posts about the "find" command line utility. Below, we'll introduce some basic practical usage to help get you started with using this handy piece of software without getting in too deep.

Reading time:
4 min

Introduction to find

The find utility can be useful to people of all skill levels that want to search for files. More skilled users could perhaps carry out some action on each file matching certain criteria. The more you learn about this powerful utility, the more searching magic you can unleash on your file system. In this post, we'll run over some of the searching basics that beginners are more likely to find useful.

Simple searching

If you want to locate a file in your filesystem called myFile.txt but have no idea where it may be, simply run the following command:

sudo find / -name myFile.txt

Note that the / tells find to start searching at the root directory level.

To find the file under your current directory, just use:

sudo find . -name myFile.txt

Where the . just stands for the current working directory

If you sort of know where the file is, you can cut out a lot of CPU cycles by only searching from the directory you know contains your file albeit in some sub directory. For example if you want to find config file called someConfigFile.conf in the /etc directory, but you're not sure where it is, run this:

sudo /etc -name someConfigFile.conf

To find any files in your home directory who's name starts with the string tutonics, you should run something like:

find ~ -name "tutonics\*"

Notice the use of the wild card * and that when you use it, you should enclose it in quotes so the bash shell doesn't try to expand it - that is if you don't use quotes, you probably won't get the result you are looking for.

Finally, take note that the above commands use sudo in order to avoid file permission error messages where you don't have required permissions. An alternative would be to just chuck the error messages in to the black hole called /dev/null like so:

find / -name myFile.txt 2>/dev/null

The easy way to explain this without scaring beginners is that: a short name for standard error in bash is simply 2, the above command basically sends or redirects anything that is an error to /dev/null so it doesn't make it to your terminal (which is where errors get displayed by default). Don't worry if you don't understand this, but if you'd like more info, we'll cover it in more detail in our bash tutorials.

From now on, we'll omit both sudo and redirecting to /dev/null to keep things simple.

Case-insensitive searching

Sometimes you'll forget whether the name of the file you want uses uppercase or lowercase letters or both. For example, if you want to find a file in your home directory called TutonicsStuff.txt, but can remember just tutonicsstuff.txt, use:

find ~ -iname tutonicstuff.txt
`Or if you remember even less, just use the wild card along with what you can remember:`
find ~ -iname "tutonics\*"

Specifying file types

You can be specific about what type of files you want to find as a result of your search. For example, to find all directories containing the word log in upper or lower case, you could use:

find . -iname "\*log\*" -type d
`Or if you only wish to find regular files containting "log" and not directories, or sockets, or pipes etc ..., you could use:`
find . -iname "\*log\*" -type f

The types you can specify after the -type are (as specified in the man page):

File typeFile type meaning

b

Block special (for example hard disk driver interface)

c

Character special (for example serial device ttyS0)

d

Directory

p

Named pipe (aka FIFO)

f

Regular file

l

Symbolic link

s

Socket

Search depth

You can tell find not to do anything until its at a certain directory depth by using the -mindepth option, for example:

find . -mindepth 2 -name "\*.txt"

This will ignore files in the current directory and only provide the matching files from sub-directories. Note that a -mindepth of 1 would find all files (except any on the command line).

Similarly, you can tell find to only provide results for a certain depth of directories. For example:

find . -maxdepth 1 -name "\*.txt"

This -maxdepth of 1 will not bother searching any sub-directories, while a maxdepth of 2 would provide any matches from the current directory and the first level of sub-directories (but not sub-directories of the sub-directories!).

Treatment of symbolic links

Find's treatment of symbolic links depends on the options -P, -L, and -H.

By default, find uses the behaviour of -P which tells find that it should never follow symbolic links.

Specifying -H will tell find not to follow symbolic links except any specified as part of the command line.

The -L option is the more interesting of the three because it tells find to follow symbolic links and to include any matches in the search results.

So lets say there is a symbolic link in your home directory to a directory called /var/tmp/mystuff, ordinarily find would not search /var/tmp/mystuff, but if you specify "-L" the link will be followed and any matches will be included in the search results.

For example, assume /home/tutonics/somedir contains a symbolic link to the dir /var/tmp/mystuff and that /var/tmp/mystuff contains the file info.txt. The below command will return info.txt from /var/tmp/mystuff directory in the results.

find -L ~ -name "\*.txt"
`while the command:`
find ~ -name "\*.txt"

will not include info.txt because the link to /var/tmp/mystuff is not followed (as per default behaviour).

Thank you for reading this article.
Please share if you liked it.