The grep command searches a file or files for lines that match a provided regular expression ("grep" comes from a command meaning to globally search for a regular expression and then print the found matches).
Structure: grep [options] regular expression [files]
To exit this command, type 0 if lines have matched, 1 if no lines match, and 2 for errors. This is very useful if you need to match things in several files.
If you wanted to find out which files in our sample directory contained the word "peppermint," you could use grep to search the directory and match those files with that word.
At the command line you would type:
grep 'peppermint' /sample/*
The * used in this example is called a meta-character, and it represents matching zero or more of the preceding characters. In this example, it is used to mean "all files and directories in this directory." So, grep will go into all the files and directories in sample and tell you which files contain "sweet." Our output will look like this:
grep: /sample/sweets: Is a directory /sample/candycanes.html
This is telling us that sweets is a directory within sample/ and grep will not search in those files for the phrase, but that candycanes.html does contain the phrase "peppermint."
See the find command for information about using grep in conjunction with find to do recursive operations.
Back to Unix Commands
- Account Log ins
Control Panel Sample
- Getting Started
Chosing a Password
Anti Spam Tips
- Creating Databases
- Unix Commands
Telnet / SSH