Linux Classes
Share This With a Friend  

How Do I Search for Files?

Linux's tree-structured file system is great for organizing your files, but a plethora of directories and subdirectories can make it easy to lose track of specific files. To search for a file named cookie, you can use the find command, like this: find / -name "cookie" -print

This command tells find to start looking in the root directory of the file system (/) for files named (-name) cookie and then to print (-print) the full name of each file it finds.

Actually, you don't need the -print flag on Red Hat Linux, but on most Unix systems, if you leave off the -print flag, find will merrily search and then throw the results in the bit bucket. (This is a fictitious device attached to all computers. When the operating system deletes a file or discards data, it is said to be thrown in the bit bucket.)

This is a departure from the rule of thumb that Linux commands will print their output on the screen unless you specify otherwise.

Starting from the root directory may take a long time, since find will search every single directory on the system. You can shorten the search time by telling find where to start searching if you know that the file you want lies along a certain path. Here are a few examples:

find . -name "cookie" -print   Start looking in the current directory.

find ~sigmund -name "cookie" -print   Start looking in sigmund's home directory.

find /etc -name "cookie" -print   Start looking in the /etc directory.

If you can't remember the exact name of the file you're after, but you have some sort of clue about it (for example, it has the word cow in it), you can use wildcards to search:

find . -name "cow*" -print   Look for files beginning with cow.

find . -name "*cow" -print   Look for files ending with cow.

find . -name "*cow*" -print Look for files with cow anywhere within them.

Finding the right files is one thing, but doing something useful with the found files would be a big plus. You can use the -exec flag to apply a command to each of the found files. The following example will send all the files it finds to the printer (lpr):

find . -name "*.txt" -exec lpr {} \;

The set of brackets ({}) is a placeholder that represents the name of each file found, and the slash-semicolon (\;) is a bit of magic that signals the end of the command. So if you had files in the current directory named sample1.txt and sample2.txt, the find command would issue the following commands for you:

lpr sample1.txt

lpr sample2.txt

You can apply any command you like to the found files.

Using find with the -exec flag is a convenient way to copy, move, print, and even delete groups of files that may be scattered across many different directories. A word of caution, though: If you're going to apply some potentially dangerous command (like rm) to your files, use the -ok flag instead of -exec. It works the same way, except it prompts you to confirm each command before executing it. Here's an example:

find . -name "*.txt" -ok mv {} junkdir \;
mv sample1.txt junkdir ok? (y/n)
mv sample2.txt junkdir ok? (y/n)

The find command found two matching files, constructed the commands shown here by substituting a file name for the brackets, and then asked for confirmation before executing the commands.

For more information on the find command, see the find manual.

Previous Lesson: Joining Files
Next Lesson: Comparing Files



Comments - most recent first
(Please feel free to answer questions posted by others!)

The Geek Scope     (26 Sep 2012, 12:55)
Thanks, Dr. Bob

Here is another useful article to supplement the information give here,

The Geek Scope - Find Files in Linux using Find Command
trumvect     (25 Nov 2011, 04:21)

Thank you very much for the following find command solution:
find . -name "*.txt" -ok mv {} junkdir \;

It's a big help as I'd been looking for this kind of solution for good number of hours(i'm a newb so tried to conceive this very idea and failed to implement it).
Just one question; what is the difference between
the aforementioned find command and the following(it also works but yours is the elegant one)
find . -iname "*searchitem*" -type f -type d -exec sh -c 'exec cp -Rf "$@" ~/junkdir' X '{}' +
I got it from net.
rrrr     (22 Nov 2011, 02:14)
*ble... how do you search for a file but you dont know where it is
Puneeth     (12 Sep 2011, 14:03)
Amazing stuff. Thank you for putting it together with necessary examples.
Brad     (12 Jan 2011, 17:50)
Wow. Thanks for the Linux find command examples. I am bookmarking your site for future use. I found you by googling Linux find examples. Great site and lots of info.
arun     (09 Jan 2011, 09:31)
am in root directory i need to find the file "sample.txt" not only the rootneed to search all the folders and sub folder also

what is the command
NoviceNotes.Net     (08 Jan 2011, 19:30)
been using Linux since “Fedora Core 5”, but have never mastered the FIND command. I'm pleased to discover this amongst the top Google finds for "easy linux find tutorial". (having only now finally broken-down to search for something outside of the man pages; for third-party resources about the GNU Coreutils FIND). Many thanks!
and to shamelessly encourage traffic, i'd like to remark i'm composing an APTITUDE tutorial, with recommendations for optimised search results using the ?expression(name) syntax
Ted     (29 Oct 2010, 09:30)
Learning Linux a bit and this site is really great man. Thanks!
Bob Rankin     (20 Sep 2010, 12:39)
@Aditya - Try this:

find . -name "*.tst" -ok ./echozero {} \;

Then create echozero script as follows:

echo 0 > $1
Aditya Pratap V.     (20 Sep 2010, 10:20)
Great article!
I have one querry though, I want to write "0" to a group of files in a particular folder. I tried -
$find . -name "*.txt" -ok echo 0 > {} \;
However this doesn't seem to work. Any suggestions?
Malavai     (17 Sep 2010, 21:55)
this is GREAT stuff - very helpful and esy to understand!!! Thanks a million Bob!!! God Bless you for all your efforts in helping new helpless Linux users such as me!
Abhishek Sharma     (23 Aug 2010, 04:41)
Amazing stuff in this site...and vary easy to understand difficult command over here.

errol garner     (06 Aug 2010, 08:18)
my linuz es 3.0 symantec autoprotect shows disable at the task bar and I am unable to restart it with the services any suggestions for a newbee
reema     (01 Aug 2010, 07:17)
Thank you very very very much for this great information,they exactly good
Kerry Hales     (15 Apr 2010, 13:17)
Thanks for a great site! It helps me NOT memorize every little aspect of Linux!

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
Notify me about new comments on this page
Hide my email

Ask Bob Rankin - Free Tech Support

Copyright © by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.