0% found this document useful (0 votes)
4 views

Unix Find command searching files

The document explains how to use the 'find' command in Unix/Linux to search for files by name, starting from a specified directory. It discusses the use of wildcards for partial matches and the '-exec' flag to perform actions on found files, as well as the '-ok' flag for safe execution of potentially dangerous commands. Additionally, it highlights the importance of specifying a starting directory to reduce search time.

Uploaded by

ABHISHEK GOUTAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unix Find command searching files

The document explains how to use the 'find' command in Unix/Linux to search for files by name, starting from a specified directory. It discusses the use of wildcards for partial matches and the '-exec' flag to perform actions on found files, as well as the '-ok' flag for safe execution of potentially dangerous commands. Additionally, it highlights the importance of specifying a starting directory to reduce search time.

Uploaded by

ABHISHEK GOUTAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

How Do I Search for Files?

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.

Read more: http://lowfatlinux.com/linux-find-files.html#ixzz1fdwcXqo2

You might also like