How to find files on Fedora

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 (-namecookie 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.

 

Vía http://lowfatlinux.com/linux-find-files.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s