Sunteți pe pagina 1din 2

Search for file with a specific name in a set of files (-name)

How to find files in Linux using 'find'

find . -name "rc.conf" -print

This command will search in the current directory and all sub directories for a file named
rc.conf.

Note: The -print option will print out the path of any file that is found with that name. In
general -print wil print out the path of any file that meets the find criteria.

How to apply a unix command to a set of file (-exec).

The exec option is probably the most important feature of the find tool. The exec
command allows you to execute a particular command on the results of the find
command. A simple demonstration of this feature is shown below. Its upto your
imagination to make maximum use of this feature. Suppose you wanted to see the
details of the files (read, write, execute permission, file size, owner etc..) that have
been returned as a search result you could do the following

$ find / - name 'Metallica*' -exec ls -l {\}\ \;

This command would find all the files on your system that begin with the letters
'Metallica' and would then execute the 'ls -l' command on these files. So basically
you would be able to see the details of the files that were returned according to your
search criteria.

The words following the -exec option is the command that you want to execute i.e. ls
-l in this case.
{\}\ is basically an indicator that the filenames returned by the search should be
substituted here.
\; is the terminating string, and is required at the end of the command

find . -name "rc.conf" -exec chmod o+r '{}' \;

This command will search in the current directory and all sub directories. All files named
rc.conf will be processed by the chmod -o+r command. The argument '{}' inserts each
found file into the chmod command line. The \; argument indicates the exec command
line has ended. The end results of this command is all rc.conf files have the other
permissions set to read access (if the operator is the owner of the file).

How to apply a complex selection of files (-o and -a).


find /usr/src -not \( -name "*,v" -o -name ".*,v" \) '{}' \; -print

This command will search in the /usr/src directory and all sub directories. All files that
are of the form '*,v' and '.*,v' are excluded. Important arguments to note are:
• -not means the negation of the expression that follows
• \( means the start of a complex expression.
• \) means the end of a complex expression.
• -o means a logical or of a complex expression.
In this case the complex expression is all files like '*,v' or '.*,v'

The above example is shows how to select all file that are not part of the RCS system.
This is important when you want go through a source tree and modify all the source
files... but ... you don't want to affect the RCS version control files.

S-ar putea să vă placă și