Searching in linux
In
Linux there are two commands for searching through files and directories.
1) Grep.
2) Find.
The grep command searches the contents of files and
can extract information from them according to the pattern specified by us. The
find command
locates files and directories according to our query.
GREP -
By
the help of grep command you
can search through files to print lines containing specified words and phrases.
And here we need not to bother to open the file with an editor.
Syntax
- grep options
expression filename
Examples of options in Grep
-v
Reverses
the normal use of the grep command - Instead of selecting lines, it rejects the
lines that match the given criteria.
-c
It
only prints the total line count of matching lines.
-i
Ignores
the case of the text when matching the given pattern.
-w
Checks
if the given pattern is a word by itself and not a part of another word.
-l
Only
gives the names of the files in which the given pattern was found
Expression is the word or phrase you want to find.
Filename is the name of the file or files you want to
search.
For
example, if you wanted to find all lines containing the word "hill"
in a file named "MLK" use -
grep hill MLK
Output –
unity% grep hill MLK
I have a dream that
one day on the red hills of Georgia, sons of...
hill and mountain
shall be made low, the rough places shall be made...
So let freedom ring
from the prodigious hilltops of New...
Let freedom ring from
every hill and molehill of Mississippi,...
unity%
To
search through a directory of files for every occurence of a word or phrase,
use the syntax grep word *
For
instance, using the command grep people * might produce a list like the following:
unity% grep people *
my_speech:people who
are self-employed.
my_speech:too few
people in too many rooms. I recommend three
my_speech:paper is
about how several people
old_speech:people
working on a project together, but a broader
from_boss:say that co.
should not be afraid of people
grep: Read error on
work_dir: Is a directory
The
name of each file containing the sought word is listed, along with the line in
that file containing the word. The last line of this example is an error
message generated when the grep command attempted to read a directory. In most cases (such as this
one) these errors can be ignored.
Searching for files (find)
The find command searches for files according to their name.
Syntax - find pathname expression
The pathname is the directory where you want the search to begin;
in most instances just put a period (.) to represent the current directory and
all subdirectories.
You should use find to search only within your own directories. If you
attempt to search someone else's, the search will take much longer and you will
be denied access to many of the directories that the find command
locates.
To search your home directory (and all directories within it) for
a file named "elmrc" you would enter
find . -name elmrc -print
unity%
find . -name elmrc -print
./.elm/elmrc
unity%
In this example
. is the pathname, the directory from which find begins
its search.
-name elmrc indicates
what find should search for (a file named "elmrc").
-print instructs
find to print the results of its search to the screen.
The find results indicate that the "elmrc" file is in
the ".elm" directory which is in the current directory.
If you're not sure what file you're looking for, you can use wildcard characters,
provided they're
enclosed in quotation marks.
For instance, to search for all files starting with the "elm"
you would enter the command
find . –name "elm*" -print
unity%
find . -name "elm*" -print
./.elm/elmrc
./.elm/elmrc.old
./misc/elm.saving
unity%
No comments:
Post a Comment