Monday, September 25, 2006

Command find

EXAMPLES

find -name 'mypage.htm'

In the above command the system would search for any file named mypage.htm in the current directory and any subdirectory.

find / -name 'mypage.htm'

In the above example the system would search for any file named mypage.htm on the root and all subdirectories from the root.

find -name 'file*'

In the above example the system would search for any file beginning with file in the current directory and any subdirectory.

find -name '*' -size +1000k

In the above example the system would search for any file that is larger then 1000k.

Here then is an example of how to search in the current directory and all subdirectories for files ending with the the extensions ".class" and ".sh" using the Unix find command:

find . -type f \( -name "*.class" -o -name "*.sh" \)

That should work on all types of Unix systems, including vanilla Unix, Linux, BSD, freeBSD, AIX, Solaris, and Cygwin.

While I'm in the neighborhood, here is an example of how to search the current directory for files that end in any of three different files extensions:

find . -type f \( -name "*cache" -o -name "*xml" -o -name "*html" \)

(FWIW, I did that one on a Mac OS/X machine.)

In these examples I always include the "-type f" option, which tells find to only display files, and specifically not directories. That's not always necessary, but when someone tells me they want to find files, I always include it.

The grep Command

The grep command is useful for finding specific character strings in a file. For example, if you want to find every reference made to "coffee" in the file sneakers.txt, you would type:

grep coffee sneakers.txt

You would see every line in that file where the word "coffee" is found.

Grep is one such tool. Let's say for example we wanted to find where
pid_t is defined. We could issue:

os161-1.10.original/kern]$ grep -irnH pid_t *

find tools
find commands

################

##Perl Script to find phrases in any file inside a directory or subdirectory

#!/bin/bash
# findstring.sh:
# Find a particular string in binaries in a specified directory.

directory=/home/jacarde1/
fstring="characters" # Here write the word you want to find.

for file in $( find $directory -type f -name '*' | sort )
do
strings -f $file | grep "$fstring" | sed -e "s%$directory%%"
# In the "sed" expression,
#+ it is necessary to substitute for the normal "/" delimiter
#+ because "/" happens to be one of the characters filtered out.
# Failure to do so gives an error message (try it).
done

exit 0
#################################################
grep smug files{search files for lines with 'smug'}
grep '^smug' files{'smug' at the start of a line}
grep 'smug$' files{'smug' at the end of a line}
grep '^smug$' files{lines containing only 'smug'}
grep '\^s' files{lines starting with '^s', "\" escapes the ^}
grep '[Ss]mug' files{search for 'Smug' or 'smug'}
grep 'B[oO][bB]' files{search for BOB, Bob, BOb or BoB }
grep '^$' files{search for blank lines}
grep '[0-9][0-9]' file{search for pairs of numeric digits}
grep '^From: ' /usr/mail/$USER{list your mail}
grep '[a-zA-Z]'{any line with at least one letter}
grep '[^a-zA-Z0-9]{anything not a letter or number}
grep '[0-9]\{3\}-[0-9]\{4\}'{999-9999, like phone numbers}
grep '^.$'{lines with exactly one character}
grep '"smug"'{'smug' within double quotes}
grep '"*smug"*'{'smug', with or without quotes}
grep '^\.'{any line that starts with a Period "."}
grep '^\.[a-z][a-z]'{line start with "." and 2 lc letters}
% grep BOB tmpfile{search 'tmpfile' for 'BOB' anywhere in a line}
% grep -i -w blkptr * {search files in CWD for word blkptr, any case}
% grep run[- ]time *.txt{find 'run time' or 'run-time' in all txt files}
% who | grep root {pipe who to grep, look for root}

egrep example
$ egrep ' paper | people ' fortunes
Line printer paper is strongest at preforations.
Man Year: 730 people working feverishly until noon.
Nudists are people who wear one-button suits.
$

Examples

To use an extended pattern that contains some of the pattern-matching characters +, ?, |, (, and ), enter:

egrep "\( *([[:lower:][:upper:]]*|[:digit:]*\)" my.txt

This displays lines that contain letters in parentheses or digits in parentheses, but not parenthesized letter-digit combinations. It matches (y) and (783902), but not (alpha19c).

Note: When using the egrep command, \ ( (backslash followed by open parenthesis) or \ ( (backslash followed by close parenthesis) match parentheses in the text, but ( (open parenthesis) and ) (closed parenthesis) are special characters that group parts of the pattern. The reverse is true when using the grep command.

Let say you want to search for string fprintf, vprintf and sprintf using grep, usually what you do is

egrep "fprintf|vprintf|sprintf" *.c

You may be ask why don’t just uses the word “printf”? If uses the word printf, it will return all of them but also include printf itself. But in this case i don’t want to grep other printf besides f,v,s printf. Thats the square brackets comes in to lessen your trouble.

egrep "[sfv]printf" *.c

It simply return the result with any character specified in [ ] with word printf concatenated.

The square brackets can be used with other RE symbols, here is another example, let say I want to gets all lists with words start with a character “a to f”, I can do this

egrep "^[a-f]" com-book.txt

It is case sensitive, I want all a to f including the upper case A to F.

egrep "^[a-fA-F]" com-book.txt

No comments: