Ubuntu/Debian/Mint news and tutorials | Linux gaming
facebook.png
twitter.png
feed.png
Quick Tip
Disable Overlay Scrollbars in GNOME
gsettings set com.canonical.desktop.interface scrollbar-mode normal
Quick Tip
Find Files Containing a Text Pattern
find . -iname "*.txt" -exec grep -l "hello" {} +
Categories
Online Readers
Advertise on TuxArena
Linux Cheat Sheet
Have a look at our Linux Cheat Sheet for quick one-liners, commands and tips.

GNU find is a powerful command-line utility that lets you search for files and folders in a hierarchical tree directory structure. It is the backend for all those utilities out there like the graphical searching in KDE or GNOME. However, find can be a little hard to handle at first by beginners. In this tutorial I will try to explain some of the capabilities of find, show some useful one-liners and provide more explanations regarding this command.

In this tutorial I will start from the basic ways of using find and head up into showing more complicated (but very useful) ways of getting the most out of it, in order to search and display exactly the results that you are looking for. The version of find that I currently have installed is 4.4.2, as it comes with Ubuntu 12.04 Precise Pangolin, and Bash 4.2.20 (older versions should work without problem too). Special thanks go to http://www.commandlinefu.com/ for some really great one-liners.

If you’re not familiar with the terminal, command-line or Linux in general I suggest you read my introductory tutorial here: Introduction to Linux Command-Line for Beginners.

The Basics

The simplest way of using find is by typying it in a terminal:

This will list all the files and folders (including hidden ones and their sub-files and sub-folders) in the current directory, following the whole hierarchical structure. This will usually generate a long list of files and doesn’t seem to give us much. It’s exactly the same as:

Where . is the currently working directory. This will list all the files and folders in the currently working directory.

It’s probably best to use a new folder somewhere in the file system to see this in effect, a folder which doesn’t have many sub-folders and files.

Moving on, let’s search for all the files that include the name profile in their filename:

* is a wildcard that replaces any number of characters or no character. The above command searches in the current folder for the name *profile*.

This will search inside /usr/share for all the files that start with FreeSans (and end in whatever characters e.g. FreeSans.ttf). I recommend using double quotes around the pattern to search for e.g. find . -name “.bash*”.

Another example:

So now you know how to search for a certain filename in a specific location.

Uppercase/Lowercase

Sometimes you need to ignore uppercase and lowercase and just search for text by ignoring case-sensitive. We’ll to this just by replacing -name with -iname:

Date

This will search for files that were created earlier than 3 days ago.

Get only the filename instead of whole path to the file

find will return the whole path to the files that match the search pattern, so in order to get only the filename you can use the printf argument:

To get only the filename, use:

By size

To search for files by size, use the -size argument, for example:

This will search inside /usr for files which are equal to or larger than 500 KB and are ending in png. Another example:

Which will search for files which are bigger than 1 MB in size. Instead of the plus sign, you could use minus in order to search for files that are smaller than a specified size:

The -10c specifier tells find to only display files which are smaller than 10 bytes. Don’t forget the + or – preceding the desired filesize.

Automatically list details about the found files

You could use a pipe and the xargs command for this:

Notice that this will list the files in the current directory if find returns no file.

Searching for files than contain specific text

This is probably one of the most useful ways to search for some file which name you’ve forgot but you know some of the text it contains inside.

This will search in all the files that contain the patter bash for the word aliases. Those files that contain this pattern will be printed out.

Some useful one-liners

Find top 20 largest files:

References

Special thanks go to http://www.commandlinefu.com/ and the following articles:

Share this!Share on RedditShare on StumbleUponShare on FacebookTweet about this on TwitterShare on TumblrShare on Google+
someuser says:

This has been the most relevant time anyone has posted tutorial for find command. All my laziness on finding cache flv files, using tools went away when I realized youtube did not offer flv on certain files. So I had to use following command inside .mozilla/firefox/whatever.default/Cache directory.

find -size +10000k | xargs vlc

to play all those videos on cache to find out which mattered. Thanks again. I was wanting to refresh my knowledge on find command once more and this article certainly is a direction ahead.

Craciun Dan says:

Great to see I could help!

David Dreggors says:

A good point on the find command that you overlook in this tip/tutorial page is that when filtering your output, you do not have to pipe to grep.

For instance you said:
“find /usr/share -name FreeSans* | grep Oblique”

that could have been less overhead in memory and easier written/read as follows:

find /usr/share -name FreeSans* -name Oblique

The find command treats 2 ‘-name’ flags as a logical “AND” which for our purpose acts like a filter (grep). To get find to match “either” case we have to separate the ‘-name’ flags by a ‘-o’ (OR) flag

Example:

$ ls -1
11
111
1122
1212
1221
2131
3131

# Find all files with a 2 in the name
$ find -name ‘*2*’
./1221
./1212
./2131
./1122

# filter only the names with 22 out of the original list
$ find -name ‘*2*’ -name ‘*22*’
./1221
./1122

Now I bet I know what you are thinking, that just did the last ‘-name’ flag and did not really use both as a filter.

Then consider this:

# Find any names with a 2 AND a 31 in the name
$ find -name ‘*2*’ -name ‘*31*’
./2131

# Now find any with a 22 AND a 31
$ find -name ‘*22*’ -name ‘*31*’

# oops… none match, but there is a 31 file right?
# There is (2131), but none that have a 22 AND 31.
# This proves the AND works.

# Obviously we would like to prove that AND
# as well as OR work…
$ find -name ‘*22*’ -o -name ‘*31*’
./1221
./2131
./1122
./3131

Viola! No need for the overhead of the external call to grep!

Also above you use a “| xargs ls -lh”… try the “-ls” flag to find, it has the same effect without the external call to xargs or ls (twice the overhead).

Craciun Dan says:

Thanks for these tips, indeed I overlooked the logical operators.

Eric says:

I believe that you can add a “-r” flag to xargs to prevent it from running when there is no input on stdin.

Craciun Dan says:

Great, that works!

Leave a Comment

Sponsors