Linux Classes
Share This With a Friend  
LINUX CLASSES - LINUX COMMANDS

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

[ RETURN TO INDEX ]


   

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

No comments yet

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.
*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 
 


Ask Bob Rankin - Free Tech Support


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