{"id":22923,"date":"2023-10-31T06:09:56","date_gmt":"2023-10-31T06:09:56","guid":{"rendered":"https:\/\/linuxways.net\/?p=22923"},"modified":"2023-10-31T06:11:56","modified_gmt":"2023-10-31T06:11:56","slug":"grep-command-in-ubuntu-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/linux-commands\/grep-command-in-ubuntu-linux\/","title":{"rendered":"grep command in Ubuntu\/Linux"},"content":{"rendered":"<p>While working with multiple files in a large system like Ubuntu, Administrators, and even Normal Users find it very challenging when searching for specific content inside a file. The usual way through GUI is by opening and accessing all the files one after the other, repeating the process until the desired content is found. This is an unreliable and time-consuming process. Thus, to make the process easier, Ubuntu offers the \u201c<strong>grep<\/strong>\u201d command which can be used in a bunch of different scenarios in the Terminal for a more efficient searching process.<\/p>\n<p>This article explains the grep command and its use in different methods in Ubuntu 22.04 LTS.<\/p>\n<h2><strong>What is the grep Command in Ubuntu\/Linux?<\/strong><\/h2>\n<p>The <strong>grep <\/strong>is a useful command for searching specific files for specific patterns or strings. The grep command accesses the files and searches for the provided query, and once the string or query is found, it returns the result in the Terminal. The grep command syntax is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"sy0\">&lt;<\/span>option<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">&lt;<\/span>word<span class=\"sy0\">\/<\/span>pattern<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>With the \u201coptions\u201d, we can modify our grep search according to our needs. The Methods covering the use of the \u201cgrep\u201d command in this article are:<\/p>\n<ul>\n<li>Searching a Single File<\/li>\n<li>Searching Multiple Files<\/li>\n<li>Searching for Multiple Words<\/li>\n<li>Searching Files with a Start Query<\/li>\n<li>Searching Files with an End Query<\/li>\n<li>Searching Files with a Start and End Query<\/li>\n<li>Searching for All Files in a Directory<\/li>\n<li>Searching Files Globally<\/li>\n<li>Searching Whole Words<\/li>\n<li>Searching Subdirectories<\/li>\n<li>Searching Files By Ignoring Case Sensitivity<\/li>\n<li>Searching Files By Excluding Set of Words<\/li>\n<li>Listing Names of Matched Files<\/li>\n<li>Listing Count Number of Matches<\/li>\n<li>Listing Line Number of Matches<\/li>\n<li>Listing Pattern Position of Matches<\/li>\n<li>Highlighting Matches with Color<\/li>\n<li>Listing Lines Containing Specific Characters<\/li>\n<li>Exporting Output into a File<\/li>\n<\/ul>\n<p>A detailed explanation of each grep method is discussed below.<\/p>\n<h2><strong>Installing the \u201cgrep\u201d Command in Ubuntu<\/strong><\/h2>\n<p>The \u201cgrep\u201d Command in Ubuntu is pre-installed, but if it is unavailable on your Ubuntu System, use the command below to install it:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">apt-get install<\/span> <span class=\"kw2\">grep<\/span><\/div><\/div>\n<p>In our case, \u201cgrep\u201d is already installed:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"732\" height=\"220\" class=\"wp-image-22924\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-1.png 732w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-1-300x90.png 300w\" sizes=\"auto, (max-width: 732px) 100vw, 732px\" \/><\/p>\n<h2><strong>Searching a Single File With the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>The grep command is usually used to search for a single file. Searching the File with a specified string to find a specific pattern through the Terminal is useful and more flexible for the Administrators as they do not have to manually open the file and then look for the string. The syntax of the \u201cgrep\u201d command to search for a single file is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"sy0\">&lt;<\/span>string<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, the Documents Directory contains the \u201cfileA.txt\u201d text file and the file contains the following content:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"895\" height=\"372\" class=\"wp-image-22925\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-2.png 895w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-2-300x125.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-2-768x319.png 768w\" sizes=\"auto, (max-width: 895px) 100vw, 895px\" \/><\/p>\n<p>To search for the string \u201cLinuxWays\u201d, the grep command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> LinuxWays fileA.txt<\/div><\/div>\n<p>The grep command will return the line containing the given string keyword, in our case, \u201cLinuxWays\u201d. The complete line returns as an output in the Terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"731\" height=\"164\" class=\"wp-image-22926\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-3.png 731w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-3-300x67.png 300w\" sizes=\"auto, (max-width: 731px) 100vw, 731px\" \/><\/p>\n<p>Similarly, to search for the line containing the string \u201cUbuntu\u201d, the same grep command will be executed as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> Ubuntu fileA.txt<\/div><\/div>\n<p>As in our case, Ubuntu was written in two lines, both lines will be returned in the Terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"111\" class=\"wp-image-22927\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-4.png 729w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-4-300x46.png 300w\" sizes=\"auto, (max-width: 729px) 100vw, 729px\" \/><\/p>\n<p>Using a similar method, you can search for a single string in a single file using the \u201cgrep\u201d Command.<\/p>\n<h2><strong>Searching Multiple Files With the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>Apart from only searching a single file, multiple files can also be searched for a specific string using the grep command. Searching for Multiple files with the grep command involves the specified string to be searched for and all the files to be searched with a syntax:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"sy0\">&lt;<\/span>string<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">&lt;<\/span>fileName1<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">&lt;<\/span>fileName2<span class=\"sy0\">&gt;<\/span> ... <span class=\"sy0\">&lt;<\/span>fileName-N<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, we have two text files, \u201cfileA.txt\u201d and \u201cfileB.txt\u201d in our \u201cDocuments\u201d Directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"949\" height=\"315\" class=\"wp-image-22928\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-5.png 949w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-5-300x100.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-5-768x255.png 768w\" sizes=\"auto, (max-width: 949px) 100vw, 949px\" \/><\/p>\n<p>The grep Command can search both files for a specified string keyword. In our case, to search for the string \u201cUbuntu\u201d, the grep command can be executed as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> Ubuntu fileA.txt fileB.txt<\/div><\/div>\n<p>The terminal displays the lines containing Ubuntu in both files. With the lines containing Ubuntu, the Terminal also indicates the file name containing that specific line:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"725\" height=\"222\" class=\"wp-image-22929\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-6.png 725w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-6-300x92.png 300w\" sizes=\"auto, (max-width: 725px) 100vw, 725px\" \/><\/p>\n<p>Similarly, to search both the files \u201cfileA.txt\u201d and \u201cfileB.txt\u201d for the string \u201cLTS\u201d, the grep Command is<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> LTS fileA.txt fileB.txt<\/div><\/div>\n<p>The Terminal will display the lines containing \u201cLTS\u201d in both files:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"734\" height=\"87\" class=\"wp-image-22930\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-7.png 734w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-7-300x36.png 300w\" sizes=\"auto, (max-width: 734px) 100vw, 734px\" \/><\/p>\n<p>Thus, using a similar method, you can search for a desired string in Multiple Files using the \u201cgrep\u201d Command.<\/p>\n<h2><strong>Searching Files With Multiple Words Using the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>The grep command can search Multiple Strings or queries at a time as well which is mostly beneficial for the system administrator. Instead of searching for one specific string and then searching for another string at a later time, both strings or queries can be searched simultaneously with the following syntax:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"st_h\">'&lt;word1&gt;\\|&lt;word2&gt;'<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, the text file \u201cfileB.txt\u201d has the following content in it:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"734\" height=\"480\" class=\"wp-image-22931\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-8.png 734w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-8-300x196.png 300w\" sizes=\"auto, (max-width: 734px) 100vw, 734px\" \/><\/p>\n<p>To search and return only the lines having the words \u201carticles\u201d or \u201cLinux\u201d, the grep command with multiple words will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"st_h\">'Linux\\|articles'<\/span> fileB.txt<\/div><\/div>\n<p>The Terminal will only display the lines either having the word \u201carticles\u201d or the word \u201cLinux\u201d. In our case, the Terminal will display the line numbers One, Two, and Four:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"731\" height=\"99\" class=\"wp-image-22932\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-9.png 731w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-9-300x41.png 300w\" sizes=\"auto, (max-width: 731px) 100vw, 731px\" \/><\/p>\n<p>Similarly, to search for the words \u201cHello\u201d and \u201cLove\u201d inside the text file \u201cfileB.txt\u201d, the grep command can be used:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"st_h\">'Hello\\|Love'<\/span> fileB.txt<\/div><\/div>\n<p>The grep command will return the lines One and Four in Terminal from the Text File:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"725\" height=\"78\" class=\"wp-image-22933\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-10.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-10.png 725w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-10-300x32.png 300w\" sizes=\"auto, (max-width: 725px) 100vw, 725px\" \/><\/p>\n<p>Using this approach, you can search for multiple words or matching patterns using the grep command inside various files.<\/p>\n<h2><strong>Searching Files With a Start Query Using the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>On some occasions, administrators require listing only the lines starting with a specific character or query. The grep command can be used to search for all the lines containing the specified character and return only the lines that match the search pattern, i.e. the provided starting character with the grep command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"st_h\">'^&lt;startingCharacter&gt;'<\/span> <span class=\"kw2\">file<\/span><\/div><\/div>\n<p>In our case, to display the lines starting with the word \u201cH\u201d inside the Text File \u201cfileB.txt\u201d, the grep command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"st_h\">'^H'<\/span> fileB.txt<\/div><\/div>\n<p>This will display all the lines with the starting word \u201cH\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"731\" height=\"58\" class=\"wp-image-22934\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-11.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-11.png 731w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-11-300x24.png 300w\" sizes=\"auto, (max-width: 731px) 100vw, 731px\" \/><\/p>\n<p>Similarly, to display the lines starting with the character \u201cI\u201d inside the Text File \u201cfileB.txt\u201d, the grep command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"st_h\">'^I'<\/span> fileB.txt<\/div><\/div>\n<p>In our case, the lines Three and Four will be displayed:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"81\" class=\"wp-image-22935\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-12.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-12.png 729w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-12-300x33.png 300w\" sizes=\"auto, (max-width: 729px) 100vw, 729px\" \/><\/p>\n<p>Using the above approach, multiple files can also be searched for querying the first character of a line inside the text files. In our case, to list the lines that start with the character \u201cI\u201d inside the Text Files \u201cfileA.txt\u201d and \u201cfileB.txt\u201d, the grep command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"st_h\">'^I'<\/span> fileB.txt fileA.txt<\/div><\/div>\n<p>The Terminal will display all the lines starting with \u201cI\u201d inside both the text files \u201cfileA.txt\u201d and \u201cfileB.txt\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"109\" class=\"wp-image-22936\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-13.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-13.png 729w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-13-300x45.png 300w\" sizes=\"auto, (max-width: 729px) 100vw, 729px\" \/><\/p>\n<p>This way the \u201cgrep\u201d command can be utilized to search various files starting with a specified query or character.<\/p>\n<h2><strong>Searching Files With an End Query Using the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>Similar to the previous method, the end query or character can also be searched using the grep command. To do that, use the below-given syntax:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"st_h\">'&lt;endingCharacter&gt;$'<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, the Text File \u201cfileB.txt\u201d inside our Documents Directory contains the following lines:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"737\" height=\"487\" class=\"wp-image-22937\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-14.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-14.png 737w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-14-300x198.png 300w\" sizes=\"auto, (max-width: 737px) 100vw, 737px\" \/><\/p>\n<p>Now, to display the lines ending with the character \u201cn\u201d, the grep command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"st_h\">'n$'<\/span> fileB.txt<\/div><\/div>\n<p>The lines ending with \u201cn\u201d will display in the Terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"80\" class=\"wp-image-22938\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-15.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-15.png 729w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-15-300x33.png 300w\" sizes=\"auto, (max-width: 729px) 100vw, 729px\" \/><\/p>\n<p>Similarly, to search for the lines ending with the character or word \u201cu\u201d inside the text file \u201cfileB.txt\u201d, the grep command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"st_h\">'u$'<\/span> fileB.txt<\/div><\/div>\n<p>The line ending with \u201cu\u201d will be displayed in the Terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"60\" class=\"wp-image-22939\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-16.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-16.png 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-16-300x25.png 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<p>Using the above approach, various files can be listed ending with a specified query.<\/p>\n<h2><strong>Searching Files With a Start and an End Query Using the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>Files can also be searched with a specific starting and ending query with the grep command. With this approach, only the lines that start and end with a specified starting and ending character or query will be searched inside the file and returned to the Terminal. The syntax to search for lines inside a file starting and ending with a specified query is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"st_h\">'^&lt;startingCharacter&gt;.*&lt;endingCharacter&gt;$'<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, using the text file \u201cfileB.txt\u201d in the previous example, we can search for lines starting with a specified query or character and ending with a specified query or character. The lines starting and ending with the specified characters will be displayed only. Using the \u201cfileB.txt\u201d file, to list the lines starting with the character or query \u201cH\u201d and ending with the character \u201c!\u201d, the grep command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"st_h\">'^H.*!$'<\/span> fileB.txt<\/div><\/div>\n<p>The First Line of the Text File \u201cfileB.txt\u201d will be displayed in the Terminal as it starts with \u201cH\u201d and ends with \u201c!\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"738\" height=\"62\" class=\"wp-image-22940\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-17.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-17.png 738w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-17-300x25.png 300w\" sizes=\"auto, (max-width: 738px) 100vw, 738px\" \/><\/p>\n<p>Similarly, to list the line starting with \u201cI\u201d and ending with \u201cu\u201d inside the Text File \u201cfileB.txt\u201d, the grep command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"st_h\">'^I.*u$'<\/span> fileB.txt<\/div><\/div>\n<p>The grep Command will return the line Four from the \u201cfileB.txt\u201d file which starts with \u201cI\u201d and ends with \u201cu\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"725\" height=\"60\" class=\"wp-image-22941\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-18.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-18.png 725w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-18-300x25.png 300w\" sizes=\"auto, (max-width: 725px) 100vw, 725px\" \/><\/p>\n<p>Using the above approach, various files can be searched for specific lines starting and ending with a specified query or character.<\/p>\n<h2><strong>Searching All Files in the Directory with the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>All the files inside a specific directory can also be searched at once with the grep command. Searching all files is useful when an administrator is dealing with multiple files. The grep command to search all the files inside a directory is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"sy0\">&lt;<\/span>string<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">*<\/span><\/div><\/div>\n<p>In our case, the Documents Directory contains three text files named \u201cfileA.txt\u201d, \u201cfileB.txt\u201d, and \u201cfileX.txt\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"945\" height=\"333\" class=\"wp-image-22942\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-19.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-19.png 945w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-19-300x106.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-19-768x271.png 768w\" sizes=\"auto, (max-width: 945px) 100vw, 945px\" \/><\/p>\n<p>Instead of using the grep to search for multiple files by typing all the file names, the asterisk \u201c<strong>*<\/strong>\u201d searches for all the files inside the Directory. The grep command in our case, to search for the string \u201cLinuxWays\u201d in all the files inside the Documents Directory will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> LinuxWays <span class=\"sy0\">*<\/span><\/div><\/div>\n<p>The file names with the lines containing the string \u201cLinuxWays\u201d are displayed:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"228\" class=\"wp-image-22943\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-20.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-20.png 736w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-20-300x93.png 300w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/p>\n<p>Similarly, to search for the string \u201clike\u201d inside all the files in our Documents Directory, the grep Command is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> like <span class=\"sy0\">*<\/span><\/div><\/div>\n<p>In our case, only one file has the string \u201clike\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"144\" class=\"wp-image-22944\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-21.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-21.png 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-21-300x59.png 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<h2><strong>Searching Files Globally that match the Pattern with the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>Files can be searched globally as well using the grep command. Searching for files globally can be beneficial if a file path is forgotten and the administrator now wants to search for a specific pattern inside the file. The grep command to search files for possible patterns or words match globally is<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-r<\/span> <span class=\"st0\">&quot;&lt;string&gt;&lt;strong&gt;&quot;<\/span><span class=\"sy0\">*&lt;\/<\/span>strong<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, to search for the string \u201cUbuntu\u201d globally, the grep command used will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-r<\/span> <span class=\"st0\">&quot;Ubuntu&quot;<\/span><span class=\"sy0\">*<\/span><\/div><\/div>\n<p>All the files on the Ubuntu System containing the string \u201cUbuntu\u201d will be returned:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"733\" height=\"386\" class=\"wp-image-22945\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-22.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-22.png 733w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-22-300x158.png 300w\" sizes=\"auto, (max-width: 733px) 100vw, 733px\" \/><\/p>\n<p>Using the procedure above, various files can be searched for a pattern globally using the grep command.<\/p>\n<h2><strong>Searching Whole Words Only in a File with the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>When we search for a pattern or string inside a file, the grep command returns the sub-string as well, which usually might be irrelevant to our search. To avoid getting the sub-strings along with your results, the \u201c-w\u201d option is used with the grep command which ensures that the grep command should only search for Whole Words. The syntax of the grep command for searching whole words is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-w<\/span> <span class=\"sy0\">&lt;<\/span>string<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>Consider our case where the \u201cfileX.txt\u201d in the Documents Directory contains the following content inside<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"733\" height=\"482\" class=\"wp-image-22946\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-23.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-23.png 733w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-23-300x197.png 300w\" sizes=\"auto, (max-width: 733px) 100vw, 733px\" \/><\/p>\n<p>Notice the word UbuntuX on the first line of the \u201cfileX.txt\u201d text file. Here the Ubuntu exists as a sub-string to the string UbuntuX:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"733\" height=\"484\" class=\"wp-image-22947\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-24.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-24.png 733w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-24-300x198.png 300w\" sizes=\"auto, (max-width: 733px) 100vw, 733px\" \/><\/p>\n<p>Using the usual grep command to list the string Ubuntu, the command used will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> Ubuntu fileX.txt<\/div><\/div>\n<p>The Terminal also displays the sub-string \u201cUbuntu\u201d of the string \u201cUbuntuX\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"733\" height=\"80\" class=\"wp-image-22948\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-25.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-25.png 733w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-25-300x33.png 300w\" sizes=\"auto, (max-width: 733px) 100vw, 733px\" \/><\/p>\n<p>The complete String \u201cUbuntuX\u201d is displayed on the Terminal although we only searched for the string \u201cUbuntu\u201d. Thus, the \u201c-w\u201d option is useful in returning only the whole word or string. In our case, to return only the Whole String Words, the grep Command used is<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-w<\/span> Ubuntu fileX.txt<\/div><\/div>\n<p>Now only the line containing the whole word \u201cUbuntu\u201d is displayed instead of returning the \u201cUbuntuX\u201d too:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"112\" class=\"wp-image-22949\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-26.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-26.png 726w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-26-300x46.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/p>\n<p>Similarly, you can search for a string pattern globally using the \u201c-r\u201d option along with the grep command. In our case, to search for the whole word string \u201cLinux\u201d globally the grep command is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-w<\/span> <span class=\"re5\">-r<\/span> <span class=\"st0\">&quot;Linux&quot;<\/span><span class=\"sy0\">*<\/span><\/div><\/div>\n<p>This will list all the lines containing the whole word \u201cLinux\u201d globally:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"733\" height=\"454\" class=\"wp-image-22950\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-27.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-27.png 733w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-27-300x186.png 300w\" sizes=\"auto, (max-width: 733px) 100vw, 733px\" \/><\/p>\n<p>Using the similar method mentioned above, you can search for other whole words either in a single file or multiple files using the grep command.<\/p>\n<h2><strong>Searching the Sub-Directories with the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>Similar to the grep command with a global search option, to search for the string pattern in files inside subdirectories the grep command is used with the \u201c-r\u201d option and asterisk \u201c<strong>*<\/strong>\u201d:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-r<\/span> <span class=\"sy0\">&lt;<\/span>string<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">*<\/span><\/div><\/div>\n<p>In our case, to search for the string \u201cLTS\u201d inside the User Directory and sub-directories, the grep command used is<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-r<\/span> LTS <span class=\"sy0\">*<\/span><\/div><\/div>\n<p>This displays all the string pattern \u201cLTS\u201d inside the files and sub-directories:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"732\" height=\"483\" class=\"wp-image-22951\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-28.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-28.png 732w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-28-300x198.png 300w\" sizes=\"auto, (max-width: 732px) 100vw, 732px\" \/><\/p>\n<h2><strong>Searching Files by Ignoring Case Sensitivity with the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>One of the problems that Ubuntu Users face while using the grep command is the case sensitivity of the string. The grep command is pre-designed to be case-sensitive and usually the Ubuntu User does not get the desired result if the string is incorrect in terms of capital or small alphabets. To avoid the issue of case sensitivity, the grep command is used with the \u201c-i\u201d flag which ignores all the case distinctions and the user can now search for a pattern or string without worrying about the capital or small alphabets. The grep command ignoring case sensitivity can be used as<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-i<\/span> <span class=\"sy0\">&lt;<\/span>string<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, using the usual grep command, to search for the string \u201cUbuntu\u201d in the file \u201cfileA.txt\u201d, the grep command used was:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> Ubuntu fileA.txt<\/div><\/div>\n<p>This returns the lines containing the word Ubuntu inside the text file \u201cfileA.txt\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"138\" class=\"wp-image-22952\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-29.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-29.png 729w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-29-300x57.png 300w\" sizes=\"auto, (max-width: 729px) 100vw, 729px\" \/><\/p>\n<p>Now if we change the search string from \u201cUbuntu\u201d to \u201cubuntu\u201d, we will get no output. The command line moves to the next line indicating no such file exists containing the word \u201cubuntu\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"725\" height=\"56\" class=\"wp-image-22953\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-30.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-30.png 725w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-30-300x23.png 300w\" sizes=\"auto, (max-width: 725px) 100vw, 725px\" \/><\/p>\n<p>Now, to ignore the case distinctions, the \u201c-i\u201d option with the grep command is used. Considering our previous example, to search for the word \u201cubuntu\u201d in the file \u201cfileA.txt\u201d, the grep command used is<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-i<\/span> ubuntu fileA.txt<\/div><\/div>\n<p>This will display the lines containing the word \u201cUbuntu\u201d indicating the grep search ignored the case sensitivity:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"137\" class=\"wp-image-22954\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-31.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-31.png 720w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-31-300x57.png 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/p>\n<p>Similarly, you can ignore the case distinction when searching for multiple files using the grep command. In our case, we will search for \u201cubuntu\u201d among all the files in the Documents Directory by ignoring the case distinctions:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-i<\/span> ubuntu <span class=\"sy0\">*<\/span><\/div><\/div>\n<p>All the file names with the lines containing \u201cubuntu\u201d or \u201cUbuntu\u201d, in our case \u201cUbuntu\u201d, are displayed. \u201cUbuntu\u201d as a sub-string is also displayed:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"291\" class=\"wp-image-22955\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-32.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-32.png 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-32-300x120.png 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<p>Similarly, using the above procedure, you can use the \u201c-i\u201d flag to ignore case distinctions and search for various words in files.<\/p>\n<h2><strong>Searching Files by Excluding a Set of Words with the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>The usual grep command with string and file name displays the occurrence of the string in the file by showing the line. The Terminal shows the string that grep searches for and the complete line. The method can also be inverted where the Terminal will display the lines that do not include the given string with the grep command. To invert the search and only display the lines excluding the given string, the \u201c-v\u201d option is used with the grep command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-v<\/span> <span class=\"sy0\">&lt;<\/span>string<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, the \u201cfileA.txt\u201d when searched for the string \u201cLTS\u201d will display the following lines using the usual grep command:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"724\" height=\"82\" class=\"wp-image-22956\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-33.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-33.png 724w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-33-300x34.png 300w\" sizes=\"auto, (max-width: 724px) 100vw, 724px\" \/><\/p>\n<p>Now to exclude the lines containing the string \u201cLTS\u201d in the \u201cfileA.txt\u201d file, the grep command used will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-v<\/span> LTS fileA.txt<\/div><\/div>\n<p>The lines inside the text file \u201cfileA.txt\u201d that do not include \u201cLTS\u201d are displayed:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"165\" class=\"wp-image-22957\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-34.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-34.png 728w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-34-300x68.png 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/p>\n<p>Similarly, you can use the inverted option to search for words without case distinctions in multiple files with the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"sy0\">&lt;<\/span>strong<span class=\"sy0\">&gt;<\/span><span class=\"kw2\">grep<\/span> <span class=\"re5\">-vi<\/span> <span class=\"sy0\">&lt;<\/span>string<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">*&lt;\/<\/span>strong<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, the command to display all the lines that exclude the word \u201cLinuxWays\u201d is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"sy0\">&lt;<\/span>strong<span class=\"sy0\">&gt;<\/span><span class=\"kw2\">grep<\/span> <span class=\"re5\">-vi<\/span> LinuxWays <span class=\"sy0\">*&lt;\/<\/span>strong<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>The \u201c-vi\u201d will ignore the case distinction and will invert the search showing only the lines that do not contain the \u201cLinuxWays\u201d or \u201clinuxways\u201d words:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"734\" height=\"309\" class=\"wp-image-22958\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-35.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-35.png 734w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-35-300x126.png 300w\" sizes=\"auto, (max-width: 734px) 100vw, 734px\" \/><\/p>\n<p>Thus, using the above process, you invert your grep search for various files.<\/p>\n<h2><strong>Listing Names of Matched Files with the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>Instead of returning the line-by-line matched patterns, the Ubuntu User can only display the files the match pattern is found in. It helps in identifying the matched files more easily and can be used as<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-l<\/span> <span class=\"sy0\">&lt;<\/span>string<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">*<\/span><\/div><\/div>\n<p>In our case, to list all the files inside the Documents Directory that includes the word or string \u201cLTS\u201d, the grep command used is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-l<\/span> LTS <span class=\"sy0\">*<\/span><\/div><\/div>\n<p>The file names will be displayed that include the word LTS in them:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"735\" height=\"131\" class=\"wp-image-22959\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-36.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-36.png 735w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-36-300x53.png 300w\" sizes=\"auto, (max-width: 735px) 100vw, 735px\" \/><\/p>\n<p>Similarly, you can search for files that contain a specific pattern globally using the \u201c-r\u201d option. In our case, to search for files globally that include the word \u201cUbuntu\u201d, the grep command is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-lri<\/span> <span class=\"st0\">&quot;Ubuntu&quot;<\/span><span class=\"sy0\">*<\/span><\/div><\/div>\n<p>The \u201c-i\u201d option with the \u201c-lr\u201d will ignore case distinction while searching for the Ubuntu word:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"732\" height=\"484\" class=\"wp-image-22960\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-37.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-37.png 732w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-37-300x198.png 300w\" sizes=\"auto, (max-width: 732px) 100vw, 732px\" \/><\/p>\n<p>The above method used in various other methods can get the files that include the specified word.<\/p>\n<h2><strong>Listing the Count Number of Matches in a File with the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>The grep command can also be useful in displaying the occurrences of the searched string in the Terminal. Usually, instead of displaying the whole matched line and then counting the searched string or query separately, the Terminal offers the use of the \u201c-c\u201d option which displays the number of occurrences of matched words in a file. To use the \u201c-c\u201d option with grep, the syntax will be<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-c<\/span> <span class=\"sy0\">&lt;<\/span>string<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, to find the number of occurrences of the word \u201cUbuntu\u201d in the file \u201cfileA.txt\u201d, the grep command used will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-c<\/span> Ubuntu fileA.txt<\/div><\/div>\n<p>The Terminal will display the numeric value of the occurrence of the specified word, in our case, Ubuntu:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"59\" class=\"wp-image-22961\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-38.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-38.png 726w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-38-300x24.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/p>\n<p>Similarly, multiple files can also be searched for the occurrence of a specified word or string with the grep command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-c<\/span> <span class=\"sy0\">&lt;<\/span>string<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">*<\/span><\/div><\/div>\n<p>In our case, to find the occurrence of the word \u201cLTS\u201d inside all the files in the Documents Directory, the grep command used will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-c<\/span> LTS <span class=\"sy0\">*<\/span><\/div><\/div>\n<p>This will display the numeric value of \u201cLTS\u201d occurrence in each file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"731\" height=\"200\" class=\"wp-image-22962\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-39.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-39.png 731w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-39-300x82.png 300w\" sizes=\"auto, (max-width: 731px) 100vw, 731px\" \/><\/p>\n<p>Thus, the above method can be used to find the occurrence of words in various different approaches.<\/p>\n<h2><strong>Listing Matched Line Numbers in Files using the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>The grep Command can also be used to display the line Number with matched words. The line that contains the specified word will have a \u201c<strong>:<\/strong>\u201d symbol before it whereas those lines that do not include the specified word will have the \u201c<strong>&#8211;<\/strong>\u201d symbol before it.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-n<\/span> <span class=\"re5\">-C<\/span> <span class=\"nu0\">2<\/span> <span class=\"sy0\">&lt;<\/span>string<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, to list the matched line number of lines having the word \u201cLinuxWays\u201d in file \u201cfileA.txt\u201d, the grep command used will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-n<\/span> <span class=\"re5\">-C<\/span> <span class=\"nu0\">2<\/span> LinuxWays fileA.txt<\/div><\/div>\n<p>The \u201c-n\u201d option will ensure showing the line number and the \u201c-C\u201d option is used for printing two lines before and after the match:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"115\" class=\"wp-image-22963\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-40.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-40.png 728w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-40-300x47.png 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/p>\n<p>The lines that include the word \u201cLinuxWays\u201d have the \u201c<strong>:<\/strong>\u201d symbol at the start and the lines with the \u201c<strong>&#8211;<\/strong>\u201d symbol at the start indicate the word does not exist in that line. In our case, the \u201cLinuxWays\u201d word is in the line \u201c2\u201d and for that reason, the line \u201c2\u201d has the \u201c<strong>:<\/strong>\u201d symbol next to it:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"733\" height=\"114\" class=\"wp-image-22964\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-41.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-41.png 733w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-41-300x47.png 300w\" sizes=\"auto, (max-width: 733px) 100vw, 733px\" \/><\/p>\n<p>Similarly, to list the Matched Line Numbers inside all the files containing the word \u201cLTS\u201d in the Documents Directory, the grep command used will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-n<\/span> <span class=\"re5\">-C<\/span> <span class=\"nu0\">2<\/span> LTS <span class=\"sy0\">*<\/span><\/div><\/div>\n<p>In our case, only Two lines include the word \u201cLTS\u201d and both line numbers have the symbol \u201c<strong>:<\/strong>\u201d next to them:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"732\" height=\"278\" class=\"wp-image-22965\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-42.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-42.png 732w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-42-300x114.png 300w\" sizes=\"auto, (max-width: 732px) 100vw, 732px\" \/><\/p>\n<p>Using the method above, you can list matched line numbers of various files.<\/p>\n<h2><strong>Listing Matched Pattern Position in Files with the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>Ubuntu files usually consist of lengthy lines which the administrators and normal users may find challenging when looking for a specific string at some position. To make the search process more efficient, grep offers the use of \u201c-o\u201d and \u201c-b\u201d options which ensure printing only the matching offset of the queried string with grep:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-o<\/span> <span class=\"re5\">-b<\/span> <span class=\"st0\">&quot;&lt;pattern\/word&gt;&quot;<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, the position of the matched pattern or word \u201cUbuntu\u201d in the text file \u201cfileB.txt\u201d can also be found with the grep command:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"731\" height=\"425\" class=\"wp-image-22966\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-43.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-43.png 731w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-43-300x174.png 300w\" sizes=\"auto, (max-width: 731px) 100vw, 731px\" \/><\/p>\n<p>To find the pattern position, the grep command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-o<\/span> <span class=\"re5\">-b<\/span> <span class=\"st0\">&quot;Ubuntu&quot;<\/span> fileB.txt<\/div><\/div>\n<p>The position of Ubuntu in the text file \u201cfileB.txt\u201d will be displayed in the Terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"730\" height=\"164\" class=\"wp-image-22967\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-44.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-44.png 730w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-44-300x67.png 300w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><\/p>\n<p>Similarly, the positions of a word can also be found in multiple files. Considering our case where we have text files in our Documents Directory. To find the position of the word \u201cLinux\u201d inside all the text files in the Documents Directory, the grep command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">-o<\/span> <span class=\"re5\">-b<\/span> <span class=\"st0\">&quot;Linux&quot;<\/span> <span class=\"sy0\">*<\/span><\/div><\/div>\n<p>The Position of \u201cLinux\u201d inside all the files in the Directory will be displayed in the Terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"193\" class=\"wp-image-22968\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-45.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-45.png 726w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-45-300x80.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/p>\n<p>Thus using the same method, the position of matched patterns or words can be found in various files.<\/p>\n<h2><strong>Using the \u201cgrep\u201d Command to Export the Output into a File<\/strong><\/h2>\n<p>Administrators are usually working with multiple files and while working with multiple files, they want all their results to be exported to another file for better accessibility. The grep command can export all the search results to another file as well with the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">bash<\/span> <span class=\"re5\">-c<\/span> <span class=\"st0\">&quot;grep -n &quot;<\/span><span class=\"sy0\">&lt;<\/span>string<span class=\"sy0\">&gt;<\/span><span class=\"st0\">&quot; * &gt; &lt;fileToSaveInto&gt;.txt<\/span><\/div><\/div>\n<p>In our case, to export the result of the \u201cUbuntu\u201d word search in all the files inside the Documents Directory, the command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">bash<\/span> <span class=\"re5\">-c<\/span> <span class=\"st0\">&quot;grep -n &quot;<\/span>Ubuntu<span class=\"st0\">&quot; * &gt; output.txt<\/span><\/div><\/div>\n<p>This will not show the result in the Terminal, instead, the result will move to the \u201coutput.txt\u201d file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"724\" height=\"112\" class=\"wp-image-22969\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-46.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-46.png 724w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-46-300x46.png 300w\" sizes=\"auto, (max-width: 724px) 100vw, 724px\" \/><\/p>\n<p>To confirm the result, open the \u201coutput.txt\u201d file with the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">nano<\/span> output.txt<\/div><\/div>\n<p>The Text Editor for the \u201coutput.txt\u201d file will open and you can see the results are in the output file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"732\" height=\"478\" class=\"wp-image-22970\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-47.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-47.png 732w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-47-300x196.png 300w\" sizes=\"auto, (max-width: 732px) 100vw, 732px\" \/><\/p>\n<p>Thus, using the same method, you can export the result of multiple files into one file.<\/p>\n<h2><strong>Highlighting the Matches with Color using the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>The grep command can also highlight the specified string with the \u201c&#8211;color\u201d command. It helps in identifying the matched string or query more easily in the Terminal as one does not have to look for the specified string in the line again. To highlight the specified string in the file, the grep command used is<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">--color<\/span> <span class=\"st0\">&quot;&lt;string&gt;&quot;<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, the Text File \u201cfileB.txt\u201d from our previous examples can be used to search and highlight a specified string. To search and highlight the string \u201cUbuntu\u201d in the file \u201cfileB.txt\u201d, the grep command with color will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">--color<\/span> <span class=\"st0\">&quot;Ubuntu&quot;<\/span> fileB.txt<\/div><\/div>\n<p>The lines containing the word \u201cUbuntu\u201d will be highlighted in the terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"133\" class=\"wp-image-22971\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-48.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-48.png 726w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-48-300x55.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/p>\n<p>Similarly, words inside multiple files can also be searched and highlighted with the grep command. In our case, to search and highlight the word \u201cLinux\u201d from the Text Files \u201cfileA.txt\u201d and \u201cfileB.txt\u201d, the grep command will be<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">--color<\/span> <span class=\"st0\">&quot;Linux&quot;<\/span> fileA.txt fileB.txt<\/div><\/div>\n<p>The grep Command will return the lines containing the string \u201cLinux\u201d and highlight them as well:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"732\" height=\"112\" class=\"wp-image-22972\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-49.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-49.png 732w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-49-300x46.png 300w\" sizes=\"auto, (max-width: 732px) 100vw, 732px\" \/><\/p>\n<p>Multiple strings can also be highlighted with the grep command. In our case, to search and highlight the strings \u201cLinux\u201d and \u201cUbuntu\u201d inside the \u201cfileB.txt\u201d file, the grep command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">--color<\/span> <span class=\"st0\">&quot;Linux\\|Ubuntu&quot;<\/span> fileB.txt<\/div><\/div>\n<p>Both the string \u201cUbuntu\u201d and \u201cLinux\u201d will be highlighted in the Terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"112\" class=\"wp-image-22973\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-50.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-50.png 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-50-300x46.png 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<p>Using the above method, various files can be searched and highlighted for various strings using the grep command.<\/p>\n<h2><strong>Listing Lines Containing a Specified Set of Characters using the \u201cgrep\u201d Command<\/strong><\/h2>\n<p>Instead of highlighting the whole string, the user or administrator can also search for a set of characters using the grep command. To search for the specific set of characters inside a file, the grep command used is<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"st_h\">'&lt;setOfCharacters&gt;*'<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, using the \u201cfileB.txt\u201d from previous methods, a specific set of characters or words can be searched and highlighted using the grep command. To search and highlight \u201cun\u201d inside the file \u201cfileB.txt\u201d, the grep command will be<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">--color<\/span> <span class=\"st_h\">'un*'<\/span> fileB.txt<\/div><\/div>\n<p>The highlighted sets of \u201cun\u201d as well as \u201cu\u201d and \u201cn\u201d individually inside the file \u201cfileB.txt\u201d will be highlighted on the Terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"112\" class=\"wp-image-22974\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-51.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-51.png 723w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-51-300x46.png 300w\" sizes=\"auto, (max-width: 723px) 100vw, 723px\" \/><\/p>\n<p>Similarly, multiple files can also be searched for a specific set of characters. To search the characters \u201ced\u201d inside the files \u201cfileA.txt\u201d and \u201cfileB.txt\u201d, the grep command will be<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">grep<\/span> <span class=\"re5\">--color<\/span> \u2018<span class=\"kw2\">ed<\/span><span class=\"sy0\">*<\/span>\u2019 fileA.txt fileB.txt<\/div><\/div>\n<p>The Terminal will highlight all the specified characters, in our case \u201ce\u201d and \u201cd\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"734\" height=\"166\" class=\"wp-image-22975\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-52.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-52.png 734w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22923-52-300x68.png 300w\" sizes=\"auto, (max-width: 734px) 100vw, 734px\" \/><\/p>\n<p>The above approach can be used in various files to search and highlight a specific character or set of characters.<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>The grep command is useful in searching for content inside a file. It can search a single file, multiple files, and global files, search based on queries, and can also list pattern positions, count the number of occurrences, and much more. This article explained all the methods used to search for patterns or strings inside a file using the grep command.<\/p>","protected":false},"excerpt":{"rendered":"<p>The grep command is useful in searching for content inside a single file, or multiple files, searching based on queries, and performing various other methods. <\/p>","protected":false},"author":110,"featured_media":22976,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1049],"tags":[],"class_list":["post-22923","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\/22923","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=22923"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22923\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/22976"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=22923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=22923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=22923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}