{"id":1019,"date":"2018-06-27T10:59:25","date_gmt":"2018-06-27T05:29:25","guid":{"rendered":"https:\/\/www.coimbatorewebhosting.com\/blog\/?p=1019"},"modified":"2019-04-16T16:05:15","modified_gmt":"2019-04-16T10:35:15","slug":"use-the-grep-commands-in-linux","status":"publish","type":"post","link":"https:\/\/coimbatorewebhosting.com\/blog\/use-the-grep-commands-in-linux\/","title":{"rendered":"How to use the GREP commands in Linux"},"content":{"rendered":"<p>How do I use grep command in Linux operating systems?<\/p>\n<p>The\u00a0grep command\u00a0which stands for \u201cglobal regular expression print,\u201d processes text line by line and prints any lines which match a specified pattern.<\/p>\n<p>It is a command line utility for searching plain text data sets for lines that match a regular expression.<\/p>\n<p>The grep command searches the given file for lines containing a match to the given strings or words. By default, grep displays the matching lines.In addition, three variant programs <strong>egrep,<\/strong> <strong>fgrep<\/strong> and <strong>rgrep<\/strong> are available. <strong>egrep<\/strong> is the same as <strong>grep -E<\/strong>. <strong>fgrep<\/strong> is the same as <strong>grep -F<\/strong>. <strong>rgrep<\/strong> is the same as <strong>grep -r<\/strong>. Direct invocation as either <strong>egrep<\/strong> or <strong>fgrep<\/strong> is deprecated, but is provided to allow historical applications that rely on them to run unmodified<\/p>\n<p>Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines.<\/p>\n<p>The name, \u201cgrep\u201d, derives from the command used to perform a similar operation, using the <a href=\"https:\/\/www.squarebrothers.com\/linux-web-hosting-india\/\" rel=\"nofollow noopener\" target=\"_blank\">Linux<\/a> text<\/p>\n<p>editor ed:<\/p>\n<pre>g\/re\/p<\/pre>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/use-the-grep-commands-in-linux\/#grep_Command_Syntax\" >grep Command Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/use-the-grep-commands-in-linux\/#How_Do_I_Use_grep_To_Search_File\" >How Do I Use grep To Search File?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/use-the-grep-commands-in-linux\/#Use_grep_recursively\" >Use grep recursively<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/use-the-grep-commands-in-linux\/#Use_grep_to_search_words_only\" >Use grep to search words only<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/use-the-grep-commands-in-linux\/#Use_grep_to_search_2_different_words\" >Use grep to search 2 different words<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/use-the-grep-commands-in-linux\/#Count_line_when_words_has_been_matched\" >Count line when words has been matched<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/use-the-grep-commands-in-linux\/#Grep_invert_match\" >Grep invert match<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/use-the-grep-commands-in-linux\/#Linux_pipes_and_grep_command\" >Linux pipes and grep command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/use-the-grep-commands-in-linux\/#How_do_I_list_just_the_names_of_matching_files\" >How do I list just the names of matching files?<\/a><\/li><\/ul><\/nav><\/div>\n<h4><span class=\"ez-toc-section\" id=\"grep_Command_Syntax\"><\/span>grep Command Syntax<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre> grep 'word' filename\r\n grep 'string1 string2' filename\r\n cat otherfile | grep 'something'\r\n command | grep 'something'\r\n command option1 | grep 'data'\r\n grep --color 'data' fileName<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"How_Do_I_Use_grep_To_Search_File\"><\/span>How Do I Use grep To Search File?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Search \/etc\/passwd for online user:<\/p>\n<pre>$ grep online \/etc\/passwd<\/pre>\n<p>You can force grep to ignore word case i.e match online, Online,ONLINE and all other combination with -i option:<\/p>\n<pre>$ grep -i \"online\" \/etc\/passwd<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Use_grep_recursively\"><\/span>Use grep recursively<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>You can search recursively i.e. read all files under each directory for a string \u201c111.100.1.1\u201d<\/p>\n<pre>$ grep -r \"111.100.1.1\" \/etc\/<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Use_grep_to_search_words_only\"><\/span>Use grep to search words only<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>When you search for online, grep will match useronline, online123, etc. You can force grep to select only those lines containing matches that form whole words i.e. match only online word:<\/p>\n<pre>$ grep -w \"online\" \/path\/to\/file<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Use_grep_to_search_2_different_words\"><\/span>Use grep to search 2 <a href=\"https:\/\/www.coimbatorewebhosting.com\/blog\/use-google-analytics-wordpress\/\" title=\"different\" alt=\"different\">different<\/a> words<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>use egrep as follows:<\/p>\n<pre>$ egrep -w 'word1|word2' \/path\/to\/file<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Count_line_when_words_has_been_matched\"><\/span>Count line when words has been matched<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>grep can report the number of times that the pattern has been matched for each file using -c (count) option:<\/p>\n<pre>$ grep -c 'word' \/path\/to\/file<\/pre>\n<p>Also note that you can use -n option, which causes grep to precede each line of output with the number of the line in the text file from which it was obtained:<\/p>\n<pre>$ grep -n 'word' \/path\/to\/file<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Grep_invert_match\"><\/span>Grep invert match<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar:<\/p>\n<pre>$ grep -v bar \/path\/to\/file<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Linux_pipes_and_grep_command\"><\/span>Linux pipes and grep command<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>grep command often used with pipes. For example print name of hard disk devices:<\/p>\n<pre># dmesg | egrep '(s|h)d[a-z]'<\/pre>\n<p>Display cpu <a href=\"https:\/\/www.coimbatorewebhosting.com\/blog\/what-is-cron-jobsscheduling-tasks-and-how-it-is-use-full-in-linux\/\" title=\"model\" alt=\"model\">model<\/a> name:<\/p>\n<pre># cat \/proc\/cpuinfo | grep -i 'Model'<\/pre>\n<p>However, above command can be also used as follows without shell pipe:<\/p>\n<pre># grep -i 'Model' \/proc\/cpuinfo<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"How_do_I_list_just_the_names_of_matching_files\"><\/span>How do I list just the names of matching files?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Use the -l option to list file name <a href=\"https:\/\/www.coimbatorewebhosting.com\/blog\/list-of-all-commands-used-in-mysql-ssh-linux-shell-in-putty\/\" title=\"whose contents\" alt=\"whose contents\">whose contents<\/a> mention main():<\/p>\n<pre>$ grep -l 'main' *.c<\/pre>\n<p>Atlast, you can force grep to display output in colors:<\/p>\n<pre>$ grep --color vivek \/etc\/passwd<\/pre>\n<p>Apart from the above if you want to delete files and folders via command SSH you can <a href=\"https:\/\/www.coimbatorewebhosting.com\/blog\/delete-files-and-folders-via-ssh\/\">click here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How do I use grep command in Linux operating systems? The\u00a0grep command\u00a0which stands for \u201cglobal regular expression print,\u201d processes text line by line and prints any lines which match a specified pattern. It is a command line utility for searching plain text data sets for lines that match a regular [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1025,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[153,17,3],"tags":[78,77,79],"class_list":["post-1019","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos-7-rhel-7","category-general","category-linux","tag-command-used","tag-global-regular-expression-print","tag-online-user"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2018\/06\/grep-commands.png","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pa2YU7-gr","_links":{"self":[{"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/comments?post=1019"}],"version-history":[{"count":0,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1019\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/media\/1025"}],"wp:attachment":[{"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/media?parent=1019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/categories?post=1019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/tags?post=1019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}