find Basics

by mike on March 14, 2011

With the Linux find utility, you can perform powerful searches on just about any criterion you can think of, and then, from the same command-line entry, invoke another utility to do whatever you need to do with the results.

In order to perform the most basic of searches, you’ll need to specify three things:

directory or search path
You can perform a search in either a specific path, or the entire filesystem.  Since find is inherently recursive, the search will automatically extend to all of the subdirectories beneath of the directory that you specify.  Of course, you can also add command switches that limit the depth of the search.

option or if you dereference symbolic links
Dereferencing means that that if find locates a symbolic link it uses the file the symbolic link points to.  However, the default option for find is to not derefence links, in other words it acts on the symbolic link.  Here are the options that can be used.
-H    if a symbolic link is specified it works with the file not the symbolic link, only effects specified symbolic links
-L    acts on the file not the symbolic link for all found
-P    acts on the symbolic link not the file for all found

expression or what you’re searching for
There are a lot of ways that you can specify this.  You can search for files of a specific name, and decide whether to make the search case-sensitive.  You can also use wildcards, or search for files with certain characteristics or that are of a certain age.  Or, you can combine multiple criteria for even more specific searches.  The main thing that limits you is your own imagination.

Here the simplest use of find searches for all files with the ending “.conf”.  The “*” acts as a wild card.  When used in the /etc directory this will search recursively through sub-directories as this is the typical function of find.

find  -iname '*.conf'
gpm-root.conf
host.conf
initlog.conf
jwhois.conf
krb5.conf
ld.so.conf

—cut—

You can also specify the directory that you would like to search.

find /etc -iname '*.conf'
/etc/gpm-root.conf
/etc/host.conf
/etc/initlog.conf
/etc/jwhois.conf
/etc/krb5.conf

—cut—

The find command can be used with other commands by piping the output of find into another command.  Here the output of find is sent to the wc command with the “l” option that counts the number of lines.
find /etc -iname '*.conf' | wc -l
23

Use find to locate specific files.  This example is using the present workign directory.
find /etc -iname 'syslog.conf'
syslog.conf

The disadvantage of using find when searching the entire file system is that hundreds of file fly by the screen befor it finds the match.  It does report the result at the end.
find . -iname 'syslog.conf'
./syslog.conf

One way to limit the screen ouput is to use tail.  This provides the last line, which is your solution.

find / -iname 'syslog.conf' | tail -n 1
syslog.conf

{ 2 comments }

lefty.crupps March 15, 2011 at 12:02 pm

Oh, 5 days on find, nice! I look forward to reading the next four days! In the mean time, a few questions:

> You can also specify the directory that you would like to search.
You say before that the directory is one of the three things we’re required to specify:
> In order to perform the most basic of searches, you’ll need to
> specify three things
but then you give an example where you don’t specify the directory, but say we can? Are we required or not to specify the directory? If we don’t specify, where does ‘find’ start at, $PWD or at / or in ~ or where?

What does the -iname flag do, does that mean the search term is ‘in the name’ or is it more like the case-insensitive ‘-i’ plus ‘-name’ ? (I know I can get this with ‘man find’ but I am asking so that you can edit and clarify your tutorial).

Thanks for Day 1!

mjf March 15, 2011 at 12:13 pm

Perhaps you should try ‘-name’ (no ‘i’) when you have a specific name?

Comments on this entry are closed.

Previous post:

Next post: