link to content
Essentials at NC State Home
skip header navigation and go to content Help | ResNet | Computing@NC State | For OIT Staff | Publications | Search NC State | Feedback | Site Map
your unity account
antivirus & security
email & messaging
connections & labs
your computer
software@nc state
files
web pages
education & training
publications
other resources
troubleshooting
ITD Sections

Searching a file or directory

Two commands for searching through files and directories are grep and find. The grep command searches the contents of files and can extract information from them. The find command locates files and directories according to names you specify.

Searching through files' contents (grep)

The grep command enables you to search through files to locate lines containing specified words and phrases without having to open the file with an editor.

The syntax for the command is

grep options expression filename

Options include

-i (ignores differences between uppercase and lowercase letters)

-v (prints all lines except those containing the specified expression)

The grep command also provides other options, which are listed in the online Unix manual pages.

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" you would enter

grep hill MLK

which would return something like

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.

The syntax for find is 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%

 

Last modified July 20, 2004 by cawalker  

jump back to content/page ends, begin footer
jump to content
jump to content Go to page top Page Top | Site Map | OIT | Policy Disclaimer | Site Survey