find - search for files and directories

Hi! If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Quite often you need to make changes to the files with known names, but unknown location in your system. Sometimes you're simply looking for a file but can only partially remember its name. In these and many other situations, find command is your friend.

Basic find command usage

The easiest form of the find command needs no additional parameters, and you get a full list of files and directories in your current directory:

$ cd /tmp 
$ find
.
./uname.txt
./.X11-unix
./.ICE-unix

Of course, find isn't limited to searching for files in your current directory, and you can easily specify which directory you want find to go through:

$ find /etc
/etc
/etc/sysconfig
/etc/sysconfig/network-scripts
/etc/sysconfig/network-scripts/ifdown-aliases
/etc/sysconfig/network-scripts/ifcfg-lo
 ...

Use find to locate files by type

Slightly more advanced form of using find allows you to specify which types of files you're interested in. As you remember, there are quite a few file types in Unix, and you can narrow your search to match exactly the type of files you're interested in.

Here's an example showing how to find all the symbolic links under a certain directory, /etc in this case:

$ find /etc -type l
/etc/sysconfig/network-scripts/ifdown
/etc/sysconfig/network-scripts/ifdown-isdn
/etc/sysconfig/network-scripts/ifup
/etc/sysconfig/network-scripts/ifup-isdn
/etc/X11/xdm/authdir
/etc/X11/xkb
...

If we were to confirm whether a certain file is indeed a symlink, it's very easy to do so:

$ ls -al /etc/X11/xkb
lrwxr-xr-x    1 root     root           27 Jul 13  2006 /etc/X11/xkb -> ../../usr/X11R6/lib/X11/xkb

As you can see, /etc/X11/xkb is a symbolic link pointing to ../../usr/X11R6/lib/X11/xkb file.

Advanced find command techniques

This section of the page will be updated from time to time, so be sure to come back to find out more!

Find files which belong to a certain Unix user

Using the -user option, you can use find command to locate all the files belonging to a certain user. In this example, I'm searching for the files which belong to a system account called rpm under the /usr/bin directory:

$ find /usr/bin -user rpm
/usr/bin/rpm2cpio
/usr/bin/gendiff
/usr/bin/rpmdb
/usr/bin/rpmquery
/usr/bin/rpmsign
/usr/bin/rpmverify
/usr/bin/rpmbuild
/usr/bin/rpmgraph

And if I want to verify that rpm really is the owner of some file from this list, I can use the ls command:

$ ls -al /usr/bin/rpm2cpio
-rwxr-xr-x    1 rpm      rpm         25568 Aug 25  2004 /usr/bin/rpm2cpio

See also:

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Fark
  • Furl
  • Netscape
  • Reddit
  • Smarking
  • Spurl
  • StumbleUpon
  • Technorati
  • YahooMyWeb

6 comments ↓

#1 How To Find Large Files and Directories in Unix | UNIX Tutorial: Learn UNIX on 03.11.08 at 5:48 am

[...] largest directories and individual files you have. This can be easily done using two Unix commands: find command and du [...]

#2 du - show disk usage stats | Unix Commands on 03.11.08 at 8:22 am

[...] you usually run du for a high-level overview, and, once the largest directories are identified, use find command to highlight the largest files within [...]

#3 How To Find a Location of a Directory in Unix | UNIX Tutorial: Learn UNIX on 04.07.08 at 12:32 pm

[...] nothing better than to employ the find command. As you might remember, among many things, this wonderful tool allows you to search files by their [...]

#4 How To List Directories in a Directory in Unix | UNIX Tutorial: Learn UNIX on 04.09.08 at 12:34 pm

[...] find command helps you show only the directories by using a -type d parameter. [...]

#5 locate - quickly find files in Linux | Unix Commands on 05.07.08 at 12:34 pm

[...] the amazing speed increase you'll get when comparing the locate command against a more traditional find command approach to locating files in [...]

#6 Locate files by Unix user id (UID) or group (GID) | UNIX Tutorial: Learn UNIX on 06.13.08 at 12:33 pm

[...] Hi! If you're new here, you may want to subscribe to the Unix Tutorial RSS feed to get regular tips & tricks for all flavors of Unix. Thanks for visiting!If you need to find all the files owned by a certain Unix user or group in a given directory, it's actually very easy to do using find command. [...]

Leave a Comment