Linux Blog

Linux find command

Filed under: Shell Script Sundays — TheLinuxBlog.com at 6:30 am on Sunday, November 7, 2010

linux find command

For those a little scared of the terminal using the Linux find command may seem a little daunting. To be honest though the find command really isn’t that hard to get the hang of. By effectively learning and using the Linux find command you’ll open up a whole new can of searching capabilities. You’ll increase your capabilities, boost productivity, and be more likely to find what your looking for. Alright, enough of the pep talk already and lets get to the core that is the powerful Linux find command.

Basic Searching

So, the first thing you need to know about the Linux find command, is how to find stuff. The simplest syntax for find is as follows:

find . -name "example_filename.txt"

What this does is find the file named (-name) example_filename.txt (“example_filename.txt”) from the present working directory “.” This may seem like a lot to find this file, but if you have a thousand directories on your desktop and you don’t know where you put that blasted example_filename.txt then it will find it. It will also do it quickly.

If you don’t remember the case of the example_filename.txt (perhaps you named it ExAmPlE_FiLeNaMe.TXT) you can use the -iname switch. You use it just like the above example.

What if you’re too lazy to cd into the directory you know the file is in. Or better put: search for files in another directory.  Well, instead of the . you can use the path. So if you want to search in /usr/share/icons you’d do

find /usr/share/icons -iname "*mail*"

Woah, what are those stars? Those are wildcards and they’re covered in the next section.

Wild Cards

So, you’ve mastered how to find a file if you know the file name, lets move on to wildcards. With the Linux Find Command, wildcards are a way of searching without knowing the everything about what you are trying to find.

The most commonly used wildcard for find is the asterisk. * will help you find what you need with only knowing part of what you wish to find.

Lets say you have a picture named example_filename.png only you don’t remember what type of picture it is, it could be .jpg, .jpeg, .gif or even the .exe that your gross aunt fanny’s infected Windows box sent you. You’d do something like this:

find . -iname "example_filename.*"

If you need to find a file where you only know the middle of a filename you can also use the asterisk at the beginning or anywhere inbetween as follows:

find . -iname "*foo*bar*"

This would match foobar.*, foodbar.* tofoobar*  tofootbarf* and so on.

Pretty simple huh? But wait there’s more! You can also use “?” as a wildcard. The question mark is a great way of finding things that you know have one certain digit. Take the following example:

You have a number of pdf files, they are sorted by a letter, followed by a number so A1.pdf through whatever, and B1.pdf through whatever, and C1.pdf through however many you want. Well, you only want the .pdf’s that have the number 2 in them, but you want all letters. You’d do something like this:

find . -iname "?2.pdf"

This is just as useful to know as the * command which I find myself using more often.

Date / Time

Handily the Linux find command also knows how to find files by date. You can search for files if you know the last accessed time, last changed time or last modified date. There are a huge number of options available with this one but the easiest is mmin (-mmin n). Make sure you represent n as a negative number.

find . -mmin -60

You can also use -mmin with a pattern

find . -mmin -60 -iname "*.doc"

Size

Finding files by size is pretty straight forward. The most common are search by kilobyte, megabyte and gigabyte these are represented respectively below.

find . -size 512k
find . -size 256M
find . -size 2G

Type

Sometimes it’s nice to be able to search just for directories, or only search for files. It can be useful for getting counts of directories or counts of files when used with wc. If you want to use the Linux find command to find all directories you can use the following:

find . -type d -iname "*test*"

The same goes for the file type, except f is used. (The following is piped to wc to get a file count)

find . -type f -iname "*test*" | wc -l

There are other types that can be used, but the ones that are useful to the average user are f (file) d (directory) and l (symlink)

Permissions

If you need to you can use the Linux find command to search for files by permission type. The easiest method is -perm and the octal representation. For more information on the octal format try: Introduction to CHMOD – Octal Format.

To simply put it you’d do something like this:

find . -perm -777

This article took me forever to write, because its hard to explain how to use the Linux Find Command in great detail. With such a powerful tool, it really is useful to read the find man page to get an understanding of the advanced options. Play with it, a lot is trial and error but with time you’ll be a master, I’m sure of it.

Pointing out Mistakes or giving Tips & Tricks are encouraged and welcomed. Please post them in the comments!