UNIX                 % find


    % whatis find
    find(1) - walk a file hierarchy
    %
find is meant to recursively search directories to find any files that meet your specified expressions.

Contents:

Introduction
find -name
find -name -exec
find -name -grep

Introduction

The find command recursively descends through the directory tree looking for files that match a logical expression.
With many options the find command might seem daunting at first but essential to know.


find ./ -name whatYouAreLookingFor -print
will print all occurrences of a file named "whatYouAreLookingFor" in the working directory and all of its subdirectories.

Arguments passed to the find command may specify either:

Search for files with a specific name: find -name

find . -name "whatYouAreLookingFor" -print

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

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.

Search & process files: find -name -exec

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

This command will search in the current directory and all sub directories for all files named whatYouAreLookingFor.
If found, the files 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 whatYouAreLookingFor files have the other permissions set to read access - assuming the operator is the owner of the file.

Search for a particular string files: find -grep

find . -exec grep "http://www.hawaii.edu" '{}' \; -print

This command will search in the current directory and all sub directories.
All files contaning the string passed as an argument to the grep command will have their path printed to standard output.

If you want to just find each file then pass it on for processing use the -q grep option. This finds the first occurrance of the search string. It then signals success to find and find continues searching for more files.

find . -exec grep -q "http://www.hawaii.edu" '{}' \; -print

This command is handy for processing a series of files that contain a specific string. You can then process each file appropriately. An example is find all html files with the string "http://www.hawaii.edu". You can then process the files with a sed script to change those occurrances of "http://www.hawaii.edu" with "http://www.uhm.hawaii.edu/".

Quaerendo invenietis!