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:
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.
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:
-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.
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:
-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 .
(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.
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
Posted on 04 May 2013. blog comments powered by DisqusRecent Posts:
How to monitor the progress of a dd process, even after it has started.
Monitoring frequency drift on the RTL SDR dongle
High entropy passwords are serious business! Here some little helpers to create better ones.
Running Linux and need to look busy quick? Try this little bit of command line fun :)
Bash scripts and command line examples are often littered with ampersands. Here's what they do.