9/18/08

find, grep

find:
NAME
find - find files
SYNOPSIS
/usr/bin/find [-H -L] path... expression
/usr/xpg4/bin/find [-H -L] path... expression
DESCRIPTION
The find utility recursively descends the directory hierar- chy for each path seeking files that match a Boolean expres- sion written in the primaries given below.
find is able to descend to arbitrary depths in a file hierarchy and does not fail due to path length limitations (unless a path operand specified by the application exceeds PATH_MAX requirements).

Examples :
find . -name "bla.log" -print
This command will search in the current directory and all sub directories for a file named bla.log.
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.

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'
find . -exec grep "size" '{}' \; -print
This command will search in the current directory and all sub directories. All files that contain the string will have their path printed to standard output.
grep:

NAME
grep - search a file for a pattern
SYNOPSIS
/usr/bin/grep [-bchilnsvw] limited-regular-expression [filename...]
/usr/xpg4/bin/grep [-E -F] [-c -l -q] [-bhinsvwx] -e pattern_list... [-f pattern_file]... [file...]
/usr/xpg4/bin/grep [-E -F] [-c -l -q] [-bhinsvwx] [-e pattern_list...] -f pattern_file... [file...]
/usr/xpg4/bin/grep [-E -F] [-c -l -q] [-bhinsvwx] pattern [file...]
DESCRIPTION
The grep utility searches text files for a pattern and prints all lines that contain that pattern. It uses a com- pact non-deterministic algorithm.
Be careful using the characters $, *, [, ^, , (, ), and \ in the pattern_list because they are also meaningful to the shell. It is safest to enclose the entire pattern_list in single quotes '...'.
If no files are specified, grep assumes standard input. Nor- mally, each line found is copied to standard output. The file name is printed before each line found if there is more than one input file.
/usr/bin/grep The /usr/bin/grep utility uses limited regular expressions like those described on the regexp(5) manual page to match the patterns.
/usr/xpg4/bin/grep The options -E and -F affect the way /usr/xpg4/bin/grep interprets pattern_list. If -E is specified, /usr/xpg4/bin/grep interprets pattern_list as a full regular expression (see -E for description). If -F is specified, grep interprets pattern_list as a fixed string. If neither are specified, grep interprets pattern_list as a basic regular expression as described on regex(5) manual page.

Example:
U want to find a string "size" in a folder and in its subfloders. This is the example
grep -i size `find . name *.* -print`

Grep can be used in so many ways and for so many reasons.
If I want to check the java processes
ps -ef grep java
If I want to check java processes owned by a particular user
ps -ef grep java grep websphe

If I want to check network statistics and specifically for a specific host
netstat -a grep hostname
..
netstat -a grep 8080

and so on.

No comments: