Linux ‘find’ command recipes

Thinking about my own work when using Linux and Unix systems, a lot of the work is based around files, and when you're working with files, tools like the Linux find command are very helpful. So, I've decided to put together this list of find command examples/recipes that I'll update from time to time when I use the find command in different ways.

How to find all files beneath the current directory that end with the .jsp extension:

find . -type f -name "*jsp"

How to find all files in the /Users/al directory that end with the .jsp extension:

find /Users/al -type f -name "*jsp"

How to find all the files (no directories) beneath the current directory and run the ls -l command on those files:

find . -type f -exec ls -l {} \;

How to find all the directories (no files) beneath the current directory and run the ls -ld command on those files:

find . -type d -exec ls -ld {} \;

Note that the d option of the ls command is needed there to keep ls from printing the contents of the directory. I often don't want that, I just want to see some attributes of the directory itself.

Find and delete

Here's how to find all files beneath the current directory that begin with the letters Poop and delete them. Be very careful with this command, it is dangerous(!), and not recommended for newbies, especially if you don't have a backup.

find . -type f -name "Poop*" -exec rm {} \;

This one is even more dangerous. It finds all directories named CVS, and deletes them and their contents. Just like the previous command, be very careful with this command, it is dangerous(!), and not recommended for newbies, or if you don't have a backup.

find . -type d -name CVS -exec rm -r {} \;

find and chmod

Here are two examples using the find command and chmod together. This first example finds all files beneath the current directory and changes their mode to 644 (rw-r--r--):

find . -type f -name -exec chmod 644 {} \;

This example shows how to find all directories beneath the current directory and changes their mode to 755 (rwxr-xr-x):

find . -type d -name -exec chmod 755 {} \;

find command aliases

I use the Unix find command so often that I usually have at least one alias created to help cut down on the typing. Here is an alias named ff ("file find"):

alias ff="find . -type f -name "

Once that alias is defined I can invoke it like this:

ff myfile.foo

Don’t forget the ‘locate’ command

If you need to find a file somewhere on the filesystem, but you can't remember where it is, but you can remember part of the filename, the locate command is often faster than the find command. Here's an example showing how to use the locate command to find a file named something like lost-file on my local system:

locate lost-file

That's all I have for now, but as mentioned, I'll add more examples/receipes as time goes by. Or, if you have your own examples you want to share, just leave a comment below.

More ‘find’ command examples

For many more examples, see my Unix/Linux ‘find’ command examples.