{"id":67,"date":"2021-08-24T16:45:00","date_gmt":"2021-08-24T16:45:00","guid":{"rendered":"https:\/\/bestdedicatedhosting.in\/blog\/?p=67"},"modified":"2021-11-02T13:13:13","modified_gmt":"2021-11-02T13:13:13","slug":"linux-grep-command-examples","status":"publish","type":"post","link":"https:\/\/bestdedicatedhosting.in\/blog\/linux-grep-command-examples\/","title":{"rendered":"Linux grep command examples"},"content":{"rendered":"\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Description<\/strong><\/p>\n\n\n\n<p><strong>grep<\/strong>&nbsp;is a powerful file pattern searcher that comes equipped on every distribution of&nbsp;<strong>Linux<\/strong>. <\/p>\n\n\n\n<p>If, for whatever reason, it is not installed on your system, you can easily install it via your package manager (<strong>apt-get<\/strong>&nbsp;on&nbsp;<strong>Debian<\/strong>\/<strong>Ubuntu<\/strong>&nbsp;and&nbsp;<strong>yum<\/strong>&nbsp;on&nbsp;<strong>RHEL<\/strong>\/<strong>CentOS<\/strong>\/<strong>Fedora<\/strong>). <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo yum install grep             #RHEL\/CentOS\/Fedora<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">1.Search and Find Files<\/h3>\n\n\n\n<p>Let\u2019s say that you have just installed a fresh copy of the new&nbsp;<strong>Ubuntu<\/strong>&nbsp;on your machine, and that you are going to give&nbsp;<strong>Python<\/strong>&nbsp;scripting a shot. <\/p>\n\n\n\n<p>You have been scouring the web looking for tutorials, but you see that there are two different versions of&nbsp;<strong>Python<\/strong>&nbsp;in use, and you don\u2019t know which one was installed on your system by the&nbsp;<strong>Ubuntu<\/strong>&nbsp;installer, or if it installed any modules. <\/p>\n\n\n\n<p>Simply run this command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># dpkg -l | grep -i python<\/code><\/pre>\n\n\n\n<p>First, we ran&nbsp;<strong>dpkg \u2013l<\/strong>, which lists installed&nbsp;<strong>*.deb<\/strong>&nbsp;packages on your system. <\/p>\n\n\n\n<p>Second, we piped that output to&nbsp;<strong>grep \u2013i<\/strong>&nbsp;python, which simple states \u201cgo to grep and filter out and return everything with \u2018python\u2019 in it.\u201d <\/p>\n\n\n\n<p>The&nbsp;<strong>\u2013i<\/strong>&nbsp;option is there to ignore-case, as&nbsp;<strong>grep<\/strong>&nbsp;is case-sensitive.<\/p>\n\n\n\n<p>Using the <strong>\u2013i<\/strong>&nbsp;option is a good habit of getting into, unless of course you are trying to nail down a more specific search.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>2.Search and Filter Files<\/strong><br><\/p>\n\n\n\n<p>The&nbsp;<strong>grep<\/strong>&nbsp;can also be used to search and filter within individual files or multiple files. Lets take this scenario:<br><\/p>\n\n\n\n<p>You are having some trouble with your&nbsp;<strong>Apache Web Server<\/strong>, and you have reached out to one of the many awesome forums on the net asking for some help. <\/p>\n\n\n\n<p>The kind soul who replies to you has asked you to post the contents of your&nbsp;<strong>\/etc\/apache2\/sites-available\/default-ssl<\/strong>&nbsp;file. <\/p>\n\n\n\n<p>Wouldn\u2019t it be easier for you, the guy helping you, and everyone reading it, if you could remove all of the commented lines? Well you can! Just run this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># grep \u2013v \u201c#\u201d  \/etc\/apache2\/sites-available\/default-ssl<\/code><\/pre>\n\n\n\n<p>The&nbsp;<strong>\u2013v<\/strong>&nbsp;option tells&nbsp;<strong>grep<\/strong>&nbsp;to invert its output, meaning that instead of printing matching lines, do the opposite and print all of the lines that don\u2019t match the expression, in this case, the&nbsp;<strong>#<\/strong>&nbsp;commented lines.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>3. Find all .mp3 Files Only<\/strong><br><\/p>\n\n\n\n<p>The&nbsp;<strong>grep<\/strong>&nbsp;can be very useful for filtering from&nbsp;<strong>stdout<\/strong>. <\/p>\n\n\n\n<p>For example, let\u2019s say that you have an entire folder full of music files in a bunch of different formats. <\/p>\n\n\n\n<p>You want to find all of the<strong>&nbsp;*.mp3<\/strong>&nbsp;files from the artist&nbsp;<strong>JayZ<\/strong>, but you don\u2019t want any of the remixed tracks. Using a&nbsp;<strong>find command<\/strong>&nbsp;with a couple of&nbsp;<strong>grep<\/strong>&nbsp;pipes will do the trick:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># find . \u2013name \u201c*.mp3\u201d | grep \u2013i JayZ | grep \u2013vi \u201cremix\u201d<\/code><\/pre>\n\n\n\n<p>In this example, we are using find to print all of the files with a&nbsp;<strong>*.mp3 extension<\/strong>, piping it to&nbsp;<strong>grep \u2013i<\/strong>&nbsp;to filter out and prints all files with the name \u201c<strong>JayZ<\/strong>\u201d and then another pipe to&nbsp;<strong>grep \u2013vi<\/strong>&nbsp;which filters out and does not print all filenames with the string (in any case) \u201c<strong>remix<\/strong>\u201d.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>4. Display Number of Lines Before or After Search String<\/strong><br><\/p>\n\n\n\n<p>Another couple of options are the&nbsp;<strong>\u2013A<\/strong>&nbsp;and&nbsp;<strong>\u2013B<\/strong>&nbsp;switches, which displays the matched line and number of lines either that come before or after the search string. <\/p>\n\n\n\n<p>While the man page gives a more detailed explanation, I find it easiest to remember the options as&nbsp;<strong>\u2013A<\/strong>&nbsp;=&nbsp;<strong>after<\/strong>, and&nbsp;<strong>\u2013B<\/strong>&nbsp;=&nbsp;<strong>before<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># ifconfig | grep \u2013A 4 eth0\n# ifconfig | grep  -B 2 UP<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. Prints Number of Lines Around Match<\/h3>\n\n\n\n<p>The grep\u2019s&nbsp;<strong>\u2013C<\/strong>&nbsp;option is similar, but instead of printing the lines that come either before or after the string, it prints the lines in either direction:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># ifconfig | grep \u2013C 2 lo<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. Count Number of Matches<\/h3>\n\n\n\n<p>Similar to piping a&nbsp;<strong>grep<\/strong>&nbsp;string to word count (<strong>wc<\/strong>&nbsp;program) grep\u2019s built-in option can perform the same for you:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># ifconfig | grep \u2013c inet6<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. Search Files by Given String<\/h3>\n\n\n\n<p>The&nbsp;<strong>\u2013n<\/strong>&nbsp;option for&nbsp;<strong>grep<\/strong>&nbsp;is very useful when debugging files during compile errors. <\/p>\n\n\n\n<p>It displays the line number in the file of the given search string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># grep \u2013n \u201cmain\u201d setup..py<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">8. Search a string Recursively in all Directories<\/h3>\n\n\n\n<p>If you would like to search for a string in the current directory along with all of the subdirectories, you can specify the&nbsp;<strong>\u2013r<\/strong>&nbsp;option to search recursively:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># grep \u2013r \u201cfunction\u201d *<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">9. Searches for the entire pattern<\/h4>\n\n\n\n<p>Passing the&nbsp;<strong>\u2013w<\/strong>&nbsp;option to grep searches for the entire pattern that is in the string. For example, using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># ifconfig | grep \u2013w \u201cRUNNING\u201d<\/code><\/pre>\n\n\n\n<p>Will print out the line containing the pattern in quotes. On the other hand, if you try:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># ifconfig | grep \u2013w \u201cRUN\u201d<\/code><\/pre>\n\n\n\n<p>Nothing will be returned as we are not searching for a pattern, but an entire word.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>10. Search a string in Gzipped Files<\/strong><\/p>\n\n\n\n<p>Deserving some mention are grep\u2019s derivatives. <\/p>\n\n\n\n<p>The first is&nbsp;<strong>zgrep<\/strong>, which, similar to&nbsp;<strong>zcat<\/strong>, is for use on&nbsp;<strong>gzipped<\/strong>files. It takes the same options as&nbsp;<strong>grep<\/strong>&nbsp;and is used in the same way:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># zgrep \u2013i error \/var\/log\/syslog.2.gz<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>11. Match Regular Expression in Files<\/strong><br><\/p>\n\n\n\n<p>The&nbsp;<strong>egrep<\/strong>&nbsp;is another derivative that stands for \u201c<strong>Extended Global Regular Expression<\/strong>\u201d. <\/p>\n\n\n\n<p>It recognizes additional expression meta-characters such&nbsp;<strong>at + ? |<\/strong>&nbsp;and&nbsp;<strong>()<\/strong>.<\/p>\n\n\n\n<p>egrep is very useful for searching source files, and other pieces of code, should the need arise. <\/p>\n\n\n\n<p>It can be invoked from regular grep by specifying the&nbsp;<strong>\u2013E<\/strong> option.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># grep \u2013E<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">12. Search a Fixed Pattern String<\/h3>\n\n\n\n<p>The&nbsp;<strong>fgrep<\/strong>&nbsp;searches a file or list of files for a fixed pattern string. <\/p>\n\n\n\n<p>It is the same as&nbsp;<strong>grep \u2013F<\/strong>. <\/p>\n\n\n\n<p>A common way of using&nbsp;<strong>fgrep<\/strong>&nbsp;is to pass a file of patterns to it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># fgrep \u2013f file_full_of_patterns.txt file_to_search.txt<\/code><\/pre>\n\n\n\n<p>This is just a starting point with&nbsp;<strong>grep<\/strong>, but as you are probably able to see, it is invaluable for a variety of purposes. <\/p>\n\n\n\n<p>Aside from the simple one line commands we have implemented,&nbsp;<strong>grep<\/strong>&nbsp;can be used to write powerful&nbsp;<strong>cron<\/strong>&nbsp;jobs, and robust&nbsp;<strong>shell scripts<\/strong>, for a start.<\/p>\n\n\n\n<p> <br>We hope you\u2019ve found this useful! <br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description grep&nbsp;is a powerful file pattern searcher that comes equipped on every distribution of&nbsp;Linux. If, for whatever reason, it is not installed on your system, you can easily install it via your package manager (apt-get&nbsp;on&nbsp;Debian\/Ubuntu&nbsp;and&nbsp;yum&nbsp;on&nbsp;RHEL\/CentOS\/Fedora). 1.Search and Find Files Let\u2019s say that you have just installed a fresh copy of the new&nbsp;Ubuntu&nbsp;on your machine, and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","footnotes":""},"categories":[4],"tags":[],"class_list":["post-67","post","type-post","status-publish","format-standard","hentry","category-linux"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/posts\/67","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/comments?post=67"}],"version-history":[{"count":1,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/posts\/67\/revisions"}],"predecessor-version":[{"id":69,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/posts\/67\/revisions\/69"}],"wp:attachment":[{"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/media?parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/categories?post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/tags?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}