My Favorite Linux Commands: find

Over the next week or so (as time allows), I’ll attempt to write a short summary of my favorite Linux commands. These are commands that I use regularly and make my life a lot easier. Hopefully, they’ll make your life easier too. Up first, the find command.

Not surprisingly, you can use the find command to locate files (and other things ;-)). What is surprising is the large number of options that you can set to find exactly the file(s) that you are looking for, and take action on them.

Let’s say that I have a directory called “sale” with JPEG files in it that have differences in their file extension (i.e, some were saved as *.jpg, and some as *.JPG) and their permission settings. I’d like to know how many of each I have, and then I’d like to change the permissions on a subset of them, to make those that are currently not readable by the “other” group readable.

First, lets see how many files are in the directory:

$ find sale/ -type f -iname "*.jpg"
sale/P9130285.JPG
sale/P9130286.JPG
sale/P9130287.JPG
sale/P9130288.JPG
sale/P9130289.JPG
sale/P9130290.JPG
sale/P9130291.JPG
sale/phone_1.jpg
sale/phone_2.jpg
sale/phone_3.jpg
sale/phone_4.jpg
sale/phone_5.jpg

Like other Linux commands, you can direct the output of find into another command to get a summary count of all the files located:

$ find sale/ -type f -iname "*.jpg" | wc -l
12

In the above example, I use the -type option and the -iname option to list all files with a .jpg extension (regardless of case). Not surprisingly, using the -name option applies a similar, but case sensitive, pattern match on file names:

$ find sale/* -type f -name "*.jpg"
sale/phone_1.jpg
sale/phone_2.jpg
sale/phone_3.jpg
sale/phone_4.jpg
sale/phone_5.jpg

There are a large number of options that can be used, and the real power of this command comes from a thorough understanding of them. One of the most useful, in my opinion, is the -exec option. This option lets you do something specific with each file that is found. To change the permissions on all files with a case sensitive “.jpg” extension and make the files readable by the “other” group, you could use the following:

find sale/* -type f -name "*.jpg" -exec chmod o+r {} ;

The -exec option lets you specify another command that is run on each file found (represented by the {} argument). Note, you also need to end the command issued by -exec with an escaped semicolon. Using the find command in this way can be enormously helpful for doing things like clearing out old log files. I typically use find in a cron job that searches directories for log files older than a certain amount of time (using the -mtime option) and then deletes them from the directory:

find logs/ -type f -name "*log*" -mtime +30 -exec rm {} ;

If you’re like me you will probably have an arsenal of commands that you use on a regular basis, to quickly and easily do the things that you do almost every day. Having the find command in your arsenal can make life a lot easier.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s