Unix: Making better use of the find command

The find command is one of the most basic Unix commands, but that doesn't mean you're making good use of its many features.


Just because a Unix command is one those most of us learn within the first few hours of our exposure to a Unix/Linux OS doesn't mean we have grasped the many ways in which it can be used to make our systems administration work easier. The find command is extremely versatile and can locate files on more criteria than many of us may realize. We can also make use of the find command for other tasks than just locating files. For example, for removing, moving, or changing files in some way.

The most obvious use of the find command is to find files by name.

$ find . -name tryme*

Putting -print at the end of your find commands is optional. But use -print0 if, for some reason, you want all the found files to be listed on a single line with no spaces between them.

$ find . -name tryme* -print0

Did you know that there's also an option that allows you to search by name independent of the case in which those file names are expressed? Try -iname instead of -name and you can do just that.

$ find . -iname tryme*

You can also use the find command to locate files by their owners or by the groups associated
with those files.

# find / -type f -user bguy
# find / -group admins
# find / -gid 99
$ find -user shs

Notice how the starting point is also optional (see the fourth command above). The find command will start its search in the local directory if no start point is provided.

Maybe you want to locate all of the symbolic links within your home directory and see what they point to (as opposed to what those directories contain)? No problem, try this:

$ find . -type l | xargs ls -ld
lrwxrwxrwx 1 shs staff 5 Dec  7 18:36 ./scripts -> ./bin
lrwxrwxrwx 1 shs staff 4 Nov 23 15:27 ./tmp -> /tmp

If we'd just run find . -type l in the above command, we'd be viewing the files in /tmp and
the local bin directory.

Or maybe we just want the names of the links to be listed and not what they point to. If so,
you could do this:

$ find . -type l -print | xargs ls -ld | awk '{print $9}'

But, of course, all we really need is this:

$ find . -type l -print

If you want to find files by type, but only want to look in top level directories, not directories, subdirectories, third level directories, etc., you can limit how far your find command will dig through your files by setting a -maxdepth limit:

$ find . -maxdepth 1 -type d

Change the 1 to a 2 if you want to search directories and subdirectories, but not go any

We can also look for files based on their ages or their size.

Photo Credit: 

flickr / Heather Blacklock, modified

Join us:






Spotlight on ...
Online Training

    Upgrade your skills and earn higher pay

    Readers to share their best tips for maximizing training dollars and getting the most out self-directed learning. Here’s what they said.


    Learn more

Operating SystemsWhite Papers & Webcasts

See more White Papers | Webcasts

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Ask a Question