Linux shell: the find command

Translation of the original in Catalan. Read it in: CatalàCastellano

Inspired by an article on BlackShell about the find command, I decided to summarise the four things discussed in the article, since they can be very useful to more than one person.

Surely many of you know the command, but because of its huge number of parameters and the laziness that often stops us from looking at the man page, many people don't even know where to start using it. A use it often gets is this one:

find . -name "*.jpg"

This searches for all the files ending in ".jpg" starting from the current directory and recursively through all the directories hanging below it. Another command I use a lot is this one:

chmod 755 `find . -type d`

It's very simple, I look for all the directories starting from the current directory and give them rwxr-xr-x (755) permissions. In the article I refer to they tell us about the following example:

find * -name "*~" -exec rm {} \;

I find this use of find more elegant than the one I used to do before; basically it's about taking advantage of find's ability to run commands when it finds what we are looking for. So, in the case of the example at hand, what we would do is look for the files ending in ~ (backup files of many editors) and it deletes them. As you can see, what it does is use the -exec parameter together with the rm (delete) command; the most important thing is to notice that {} refers to the name of the file found and that \; is used to define the end of the command.

A quick lesson on find: if you really liked it and found it useful, you already have a good reason to look at the man page and take advantage of all its power.