BashItOut

How To Use The GNU - Linux Find Command

The find command is an awesomely powerful, and often under appreciated tool and this article will introduce the command and its basic usage.

If you already comfortable with find and are looking for example usage, try Find Examples For Linux.


Lets get started with the most basic usage:

$ find
.
./.bashrc
./.profile

Provided with no arguments, find will list all files and directories in the current working directory and any of its subdirectories recursively (so any of their subdirectories etc.) one line per item and it will list the relative path


The first argument to give find would be the path to the directory you wish to start its hunt for files.

$ find /etc/apache2/sites-enabled/
/etc/apache2/sites-enabled/
/etc/apache2/sites-enabled/000-default

Here we pass find the path to Apache’s sites-enabled directory and it lets us know there’s only the default VHost enabled.


Find has a mass of available arguments, expressions and options. allowing you to filter and manipulate the results, and making it a very versatile tool. They normally begin with a single dash, like the most common -name .

Let’s start with the following file structure of a very basic website in our web root, and use find to poke around.

/var/www/index.html
/var/www/about/index.html
/var/www/about/full-staff-photo.png
/var/www/style/style.css
/var/www/contact/index.html
/var/www/contact/map.png

The -name option takes a string as an argument and will filter the results in a fairly crude manner. So to find all the files called “index.html”, we can use:

$ find /var/www -name index.html
/var/www/index.html
/var/www/about/index.html
/var/www/contact/index.html

-name also accepts wildcards with the * symbol which will match any character any number of times.

It’s worth noting that when you use a wildcard here, it’s a good idea to wrap the string in double quotes to avoid unexpected interpolation from your shell.

$ find /var/www -name "*.png"
/var/www/about/full-staff-photo.png
/var/www/contact/map.png

Wildcards can be used multiple times as well, so if you remember you have an image that had the word “staff” in it, you could find it with:

$ find /var/www -name "*staff*.png"
/var/www/about/full-staff-photo.png

-name is case sensitive, but has a twin sister, -iname who is identical in every way except she is case __I__nsensitive.

Very useful if you have users who like to CapiTaLiSe FiLEs, just be aware that it will be slower when searching large sets of files.


Basic searches can be done with -name, but for anything a little more elaborate, we can use the power of Regular Expressions with the -regex option. This comes with an accompanying case insensitive -iregex option.

Note that find uses Emacs style Regex by default and they may not be what you’re used to. I prefer to switch them to a more familiar egrep syntax using the flags -regextype posix-egrep .

$ find /var/www -regextype posix-egrep \
-regex ".*/[a-z]{5}\.\w+"
/var/www/index.html
/var/www/style/style.css
/var/www/about/index.html
/var/www/contact/index.html

(I’ve formatted the command over 2 lines here here by using a backslash for ease of readability)


-exec allows you to execute commands directly with find, based around the results it returns.

$ find /var/www -name "*.html" -exec wc -l {} \;
4 /var/www/index.html
94 /var/www/about/index.html
69 /var/www/contact/index.html

The exec option takes the command to run as its argument, and you can add in the output from find using the empty braces {} .

Your exec command also needs to be terminated by a semi-colon, which will in turn need escaping by your shell, hence the \; .


Find has a massive amount of options and tests you can use. Some common options are to filter results by the time files were accessed or modified, or the size of the files.

Many of these are covered by examples in my post Find Examples For Linux


Tags: Linux

blog comments powered by Disqus

About

@MTerzza Twitter Icon

+MikeTerzza Google Plus Icon

Atom | RSS RSS Icon

Recent Posts:

RTL SDR Frequency Drift Offset

Random Seriousness with Python and Password Generation

Random Fun and the Busy Linux Geek

Ampersands & on the command line

Killing processes in Linux