Learning Linux: Finding Files
As I continue my “Learning Linux” series, I would like to cover some commands that will be helpful for finding files. Sometimes, you know a certain file exists, but you can’t seem to remember where. Unlike systems with a graphical user interface (GUI), there is no search field you can type to find what you’re looking for. However, you can instead use the
The command in one of its most basic forms looks like this:
find [path] -name [file]. This basically means “find in this path this filename”, and the paths to the matching files will output. The interesting thing about this is that it is recursive, meaning it will find all matches from the path you specified on down through all subdirectories. For example, from the current directory, you know there are many .jpg files, and you need to locate them all. You can run
find . -name “*.jpg”. The period simply represents the present directory you are in, and “*.jpg” is a wildcard that will find any files ending with .jpg. This command then reads as “find from the current directory (and all subdirectories) any files that end in .jpg”. You will then have a list of all files with that extension printed to your screen.
Another useful variant is the similar
find [path] -iname [file]. Since Linux is case sensitive, “-iname” will simply ignore whatever case you enter, and return any matches. This is of course helpful if you don’t recall the exact filename down to the letter case, or if you need to find all letter case variants.
Specifying types and sizes
find will return file and directory matches, you can specify if you only want file matches to return:
find [path] -type f -name [file]. The only addition here is
-type f, which does exactly what it looks like: only looks for files. On the other hand, if you want directories only, you would use
-type d instead.
Another way to narrow down results is to use
-size. This will allow you to search for files smaller than, greater than, or that match exactly certain sizes. For example, to find files over 100Mb, you would use
find [path] -size +100M -name [file]. The opposite would be to find files smaller than 100Mb:
-size -100M. And lastly, finding files that are exactly 100Mb would simply be
-size 100M. Other size options can be found by using
man find, under the size options.
All of these options can be strung together. For example:
find /var -type f -size +10Mb -iname “*log*”. This will find from the /var directory and its subdirectories files larger than 10Mb that contain “log” in their name, regardless of letter case.
These options show the basics for the
find command, and hopefully will help you in managing your Linux machine and streamlining the management of your systems.