Top 10 Grep Command in Linux
Grep is a popular and powerful command in Linux and Unix operating systems.
Each developer and QA in software development need to know about grep command usage.
Questions on Grep are asked by almost all interviews for the developer/QA role. Everyone in the Software field must know the Grep Usage and commands. Grep is used to search in a file or multiple files for matching keywords or lines. Grep searches for a word and return the line in the set of one or more files. Grep command can be input to some other command and Grep can be used in a variety of ways as follows.
Grep Command usage syntax:- grep ‘word’ /file/name or /directory/path Below is the sample file for all grep examples. $ cat cloud.txt This tutorial is about to grep command in Linux and Unix usage Grep is a powerful search tool for searching words in a set of files Grep can be used with other Linux commands Grep is so fast compared to other commands in Linux systems.
How to search for a word in a file
$ $ grep ‘word’ cloud.txt Grep is a powerful search tool for searching words in a set of files
Keyword ‘word’ searched in cloud.txt returns the matching line which has ‘word’. By default grep without any options searches for files if the file name is specified, and directories, not subdirectories if the directory specified
How to do case insensitive search for a word in a file.
By default Grep command in Linux or Unix treats each case-sensitive word as different. To do a case-insensitive search, the Grep command has the option -i
grep -i "Grep" cloud.txt
It prints all lines in the cloud.txt file as grep variations (Grep, grep) are presented in each line of the file.
Count of words matched in a file
grep -c "Grep" cloud.txt
And the output is 3 if we add option – I and the output is 4.
Recursive search using grep command
By default Grep command searches files in current directories, To do subdirectories, have to use the -r option
grep -r "Grep" /usr/cloud
Please click +1 if you like this post.