{"id":21897,"date":"2023-10-01T04:11:52","date_gmt":"2023-10-01T04:11:52","guid":{"rendered":"https:\/\/linuxways.net\/?p=21897"},"modified":"2023-10-01T04:11:52","modified_gmt":"2023-10-01T04:11:52","slug":"how-to-find-text-in-files-using-the-linux-grep-command","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/linux-commands\/how-to-find-text-in-files-using-the-linux-grep-command\/","title":{"rendered":"How to Find Text in Files Using the Linux Grep Command?"},"content":{"rendered":"<p><strong>Grep (Global Regular Expression Print)<\/strong> is a powerful Unix\/Linux command line tool widely used by network administrators, network engineers, etc. for searching string patterns in log files. In this article, we will demonstrate different ways of searching and filtering string patterns in files using the grep command in Ubuntu 22.04 LTS.<\/p>\n<h2><a id=\"post-21897-_ljks07h0rocp\"><\/a>Prerequisites<\/h2>\n<p>To follow this article, we will require<\/p>\n<ul>\n<li>System with Ubuntu 22.04 LTS Installed.<\/li>\n<li>Basic knowledge of command line usage.<\/li>\n<\/ul>\n<h2><a id=\"post-21897-_vwi4ezl9toa3\"><\/a>What is the grep Command?<\/h2>\n<p><strong>Grep<\/strong> is a Unix\/Linux command line tool used to search and filter out string patterns in a file or streams, etc. \u201cgrep\u201d is very useful in searching through large log files and streams.<\/p>\n<h2><a id=\"post-21897-_i8u949r5a7ak\"><\/a>How to Find\/Search Text in Files via grep Command?<\/h2>\n<p>We will discuss different ways to filter out string patterns using the Linux grep command in the following sections:<\/p>\n<h2><a id=\"post-21897-_d6g1gvpwhut2\"><\/a>How to Find\/Search a String Pattern in a File?<\/h2>\n<p>If we would like to search for a string pattern in a file we can use the following syntax:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">grep<\/span> <span class=\"sy0\">&lt;<\/span>string<span class=\"sy0\">&gt;<\/span> <span class=\"kw2\">file<\/span><\/div><\/div>\n<p>We can replace the string with the desired pattern and mention either the file name or the path of the file in the syntax. For example, the following command filters out the string pattern \u201ccongestion\u201d in \u201csamplefile1.txt\u201d:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">grep<\/span> Congestion Downloads<span class=\"sy0\">\/<\/span>samplefile1.txt<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"547\" class=\"wp-image-21898\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-1.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-1-300x80.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-1-1024x274.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-1-768x205.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-1-1536x410.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p>From the above image, it can be seen that the grep command has filtered out the desired string pattern, i.e., \u201cCongestion\u201d by highlighting all the occurrences. Similarly, we can navigate to a specific directory, if we would like to search using the file name. An example is as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw3\">cd<\/span> Downloads<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"387\" class=\"wp-image-21899\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-2.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-2-300x57.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-2-1024x194.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-2-768x145.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-2-1536x290.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p>Now that we have navigated to the directory containing samplefile1, we can use the below command to search \u201cCongestion\u201d:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">grep<\/span> Congestion samplefile1.txt<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"622\" class=\"wp-image-21900\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-3.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-3-300x91.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-3-1024x311.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-3-768x233.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-3-1536x467.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p>Additionally, if we would like to search multiple words, we can search by enclosing them in double quotation marks, i.e., \u201c \u201c. For example, if we would like to search \u201cNetwork Congestion\u201d in samplefile1.txt, we will run the following command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">grep<\/span> <span class=\"st0\">&quot;Network Congestion&quot;<\/span> samplefile1.txt<\/div><\/div>\n<h2><a id=\"post-21897-_d9qfoolcka6p\"><\/a><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"319\" class=\"wp-image-21901\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-4.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-4-300x47.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-4-1024x160.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-4-768x120.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-4-1536x239.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/h2>\n<p>From the above output, it can be seen that all the lines in the file containing the string \u201cNetwork Congestion\u201d are filtered out.<\/p>\n<h2><a id=\"post-21897-_9r9h0y6kmc6i\"><\/a>How to Find\/Search a String Pattern in Multiple Files?<\/h2>\n<p>We can search a string pattern in multiple files using the below syntax:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">grep<\/span> <span class=\"sy0\">&lt;<\/span>string<span class=\"sy0\">&gt;<\/span> file1 file2 file3<\/div><\/div>\n<p>The below example shows the occurrences of the string \u201cCongestion\u201d in three files, i.e., samplefile1, samplefile2, and samplefile3:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">grep<\/span> Congestion samplefile1.txt samplefile2.txt samplefile3.txt<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"1008\" class=\"wp-image-21902\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-5.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-5-300x148.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-5-1024x504.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-5-768x378.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-5-1536x756.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"350\" class=\"wp-image-21903\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-6.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-6-300x51.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-6-1024x175.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-6-768x131.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-6-1536x263.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p>We can see from the above image that all occurrences of the string \u201cCongestion\u201d in selected three files are highlighted.<\/p>\n<h2><a id=\"post-21897-_w1r3fpgj9rjb\"><\/a>How to Find\/Search a String Pattern in Sub-directories?<\/h2>\n<p>We can search for a string pattern in all subdirectories by adding the \u201c-r\u201d flag followed by the string to search:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">grep<\/span> <span class=\"re5\">-rw<\/span> congestion <span class=\"sy0\">*<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"1146\" class=\"wp-image-21904\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-7.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-7-300x168.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-7-1024x573.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-7-768x430.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-7-1536x860.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p>From the above image, it can be seen that occurrences of a string in all sub-directories are highlighted. Further \u201c-w\u201d flag is added to find whole world occurrences.<\/p>\n<h2><a id=\"post-21897-_z1682vbm636g\"><\/a>How to Find Whole Words Only in Files?<\/h2>\n<p>In order to find the whole word, we can use the flag \u201c-w\u201d with the grep command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">grep<\/span> <span class=\"re5\">-w<\/span> Congestion <span class=\"sy0\">*<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"1092\" class=\"wp-image-21906\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-8.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-8-300x160.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-8-1024x546.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-8-768x410.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-8-1536x819.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p>From the above image, it can be seen that grep has highlighted all occurrences of the searched string, i.e., Congestion.<\/p>\n<h2><a id=\"post-21897-_8whgiie8opvb\"><\/a>How to Ignore Case in Grep Searches?<\/h2>\n<p>Grep searches are case-sensitive by default. In case, we would like to search for a string and ignore the case, we can search by adding the flag \u201c-i\u201d followed by a string to search:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">grep<\/span> <span class=\"re5\">-i<\/span> congestion <span class=\"sy0\">*<\/span><\/div><\/div>\n<h2><a id=\"post-21897-_nzad01fzsj7t\"><\/a><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"944\" class=\"wp-image-21907\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-9.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-9-300x138.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-9-1024x472.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-9-768x354.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-9-1536x708.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/h2>\n<p>The above image shows all occurrences of string \u201ccongestion\u201d irrespective of the case.<\/p>\n<h2><a id=\"post-21897-_majrof9u0vk3\"><\/a>How to Identify Lines that Exactly Match the Searched String?<\/h2>\n<p>The grep command searches the entire line that completely matches a string pattern by adding a \u201c-x\u201d flag. An example of a search of the \u201cNetwork Congestion\u201d string in a file is shown below:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">grep<\/span> <span class=\"st0\">&quot;Network Congestion&quot;<\/span> <span class=\"sy0\">*<\/span><\/div><\/div>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">grep<\/span> <span class=\"re5\">-x<\/span> <span class=\"st0\">&quot;Network Congestion&quot;<\/span> <span class=\"sy0\">*<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1816\" height=\"788\" class=\"wp-image-21908\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-10.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-10.png 1816w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-10-300x130.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-10-1024x444.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-10-768x333.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-10-1536x667.png 1536w\" sizes=\"auto, (max-width: 1816px) 100vw, 1816px\" \/><\/p>\n<p>In the above image, we can see that if we search \u201cNetwork Congestion\u201d, we find few matches, but when we search the same for a complete line match using the \u201c-x\u201d flag, there is no match in the file.<\/p>\n<h2><a id=\"post-21897-_7i60r9yqjcvh\"><\/a>How to List the Name of Matching Files that Contains the Searched String?<\/h2>\n<p>grep enables the searching of files containing a certain string pattern. An example is as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">grep<\/span> <span class=\"re5\">-l<\/span> throughput <span class=\"sy0\">*<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1868\" height=\"222\" class=\"wp-image-21909\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-11.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-11.png 1868w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-11-300x36.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-11-1024x122.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-11-768x91.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-11-1536x183.png 1536w\" sizes=\"auto, (max-width: 1868px) 100vw, 1868px\" \/><\/p>\n<p>From the above image, we can see that samplefile1.txt and samplefile2.txt contain the string \u201cthroughput\u201d.<\/p>\n<h2><a id=\"post-21897-_gp2tp75lw364\"><\/a>The Count of Number of Matches<\/h2>\n<p>The grep command allows one to count the number of matches in a file. The below example shows the count of occurrences of the string \u201cnetwork\u201d in a directory:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">grep<\/span> <span class=\"re5\">-c<\/span> network <span class=\"sy0\">*<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1888\" height=\"502\" class=\"wp-image-21910\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-12.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-12.png 1888w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-12-300x80.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-12-1024x272.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-12-768x204.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-21897-12-1536x408.png 1536w\" sizes=\"auto, (max-width: 1888px) 100vw, 1888px\" \/><\/p>\n<p>From the above image, we can see that the word \u201cnetwork\u201d has appeared 11, 23, and 11 times in samplefile1.txt, samplefile2.txt, and samplefile3.txt respectively.<\/p>\n<h2><a id=\"post-21897-_k4r3ojha6hv3\"><\/a>Conclusion<\/h2>\n<p>The Linux grep command is used extensively to search and filter out string patterns in a file, multiple files, sub-directories, etc. For example, searching part of a word, whole word, exact matching string, case-sensitive search, etc. Additionally, the grep command can filter out all the files that contain a particular string pattern and count occurrences of a string in files. In this article, different ways of searching string patterns in file(s) using the grep command were discussed.<\/p>","protected":false},"excerpt":{"rendered":"<p>The Linux grep command is used extensively to search and filter out string patterns in a file, multiple files, sub-directory, etc. <\/p>","protected":false},"author":110,"featured_media":21905,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1049],"tags":[],"class_list":["post-21897","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux-commands"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/21897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=21897"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/21897\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/21905"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=21897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=21897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=21897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}