find – search for files and directories

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.

Linux find Command

In case you’re looking for a way to find a specific command in Linux, please have a look at the following: apropos and whatis.

For examples of using the find command in Unix and Linux, please continue reading.

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

Basic commands in Unix Advanced Unix commands touch command mkdir command How To: find large directories in Unix/Linux

greys@centos7:~ $ getent group mail
mail:x:12:postfix

See also




Keep Learning

Follow me on Facebook, Twitter or Telegram:
Recommended
I learn with Educative: Educative
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!

Recent Tweets