How Do I Search for Files?
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!)
Here is another useful article to supplement the information give here,
The Geek Scope - Find Files in Linux using Find Command
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.
what is the command
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
find . -name "*.tst" -ok ./echozero {} \;
Then create echozero script as follows:
echo 0 > $1
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?
Thanks.
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.
Copyright © by -
All rights reserved - Redistribution is allowed only with permission.

