grep command is for the program for usage in command line for UNIX and and Unix derivatives which is used to search and filter strings from from a junk of files.It was originally developed by Ken Thompson in 1973. The term grep stands for g lobal / r egular e xpression / p rint or g lobal search for a r egular E xpression and p rint out matched lines : “global search for a regular expression and print matching lines.” Historically, the name grep or grep command evolved from the command g / re / p from the standard UNIX editor ed. There are many variants such as egrep, fgrep and agrep.
Introduction to grep command and program
On grep command, a small website can be created. The following articles falls among Suggested Reading for the new users :
- Linux Commands and UNIX Commands for Server
- List of Indispensable OS X Commands
- UNIX Operating System
- Unix like Operating System
Grep command comes under BSD General Commands Manual and documentation can be found on Apple’s developer portal :
In the command line grep is used mostly for searching files. Grep command pick out rows in which the pattern is included as a word, the line numbers, and output the file names of all files that contain the specified text. As written on manual page :
grep, egrep, fgrep, zgrep, zegrep, zfgrep -- file pattern searcher
In other words, you can search for a file, a folder, a word inside a text file etc. Regular expressions can be used with grep command. Output can be saved as usually.
Examples of usage of grep command
man means manual, so running the man grep command in command line will give you a basic usage’s manual. If I want to search for log folder in the server, I can run using this pattern :Advertisement
grep term-to-search where-to-search
it will become :
grep log *
It will not only show the locations but also tell me whether they are directories or files. This is useful if you can not find a folder on server. In WordPress forum, new users often asked to locate the php log folder – they asks as cross question – where is log folder ? The ‘WordPress Developers’ has excellent knowledge (pun intended), they replies – “ask your webhost”. You can simply put a php script to find the log folder – grep command can be scripted and not necessarily needs shell access (like curl, wget etc.). Technically it can be used for searching webpages creating a script. Note : recursive (-r) should be added to searching the directories. The above is a simple example.
Now, take that I found the log file. But I want to know how many times the digit 500 has been shown, -c is for count :
grep -c "500" log.txt
Now I noticed that, in the 500 -ed result, there is need to exclude some results. That can be done. We can search a device name too. We can use color :
grep -r Abhishek /Users --color
Avoid using -i and -r ; it can create logical clash depending on the command. But my mother also wants to search with the Same Word, so in that case I will save the search as text file :
grep (put your options) >> filename.txt