
{"id":55,"date":"2023-10-06T12:36:58","date_gmt":"2023-10-06T12:36:58","guid":{"rendered":"http:\/\/pi-smb.dsslit.com\/wordpress\/?p=55"},"modified":"2023-10-06T12:36:58","modified_gmt":"2023-10-06T12:36:58","slug":"find-text-in-files-using-the-grep-command","status":"publish","type":"post","link":"https:\/\/dsslit.ddns.net\/wordpress\/?p=55","title":{"rendered":"Find text in files using the grep command"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Using grep, you can quickly find text matching a regular expression in a single file, a group of files, or text coming from stdin.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Searching for patterns of text in files or text streams is one of the most common tasks you&#8217;ll perform in your sysadmin career. This is a valuable skill that allows you to check a variety of system configurations, <\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The most common way to find text in a Linux system is using the command-line utility&nbsp;<code>grep<\/code>. This utility was originally developed for the Unix operating system in the early 1970s. Grep evolved over the years, and the most common version available today for Linux, GNU grep, has additional features such as colored output. However, its main functionality is still the same.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using&nbsp;<code>grep<\/code>, you can quickly find text matching a&nbsp;<a href=\"https:\/\/www.redhat.com\/sysadmin\/introducing-regular-expressions\" target=\"_blank\" rel=\"noreferrer noopener\">regular expression<\/a>&nbsp;in a single file, a group of files, or text coming from&nbsp;<a href=\"https:\/\/www.redhat.com\/sysadmin\/redirect-operators-bash\" target=\"_blank\" rel=\"noreferrer noopener\"><code>stdin<\/code><\/a>&nbsp;using the shell pipe operator.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This article covers how to use the&nbsp;<code>grep<\/code>&nbsp;command to find text.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Find text in a file<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The most basic way to use&nbsp;<code>grep<\/code>&nbsp;is searching for text in a single file. To do this, type&nbsp;<code>grep<\/code>&nbsp;followed by the text pattern to search for and the file name to search in. For example, to find which port the Secure Shell (SSH) daemon uses, search for&nbsp;<code>Port<\/code>&nbsp;in file&nbsp;<code>\/etc\/ssh\/sshd_config<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ grep Port \/etc\/ssh\/sshd_config\nPort 22\n<em>#GatewayPorts no<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Notice that&nbsp;<code>grep<\/code>&nbsp;finds all lines that match the text pattern regardless of where the pattern is located.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>[ Download the&nbsp;<a href=\"https:\/\/opensource.com\/downloads\/grep-cheat-sheet?intcmp=701f20000012ngPAAQ\" target=\"_blank\" rel=\"noreferrer noopener\">Linux grep command cheat sheet<\/a>. ]<\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Extend grep with regular expressions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the previous example, when you searched for&nbsp;<code>Port<\/code>&nbsp;in the SSH configuration file,&nbsp;<code>grep<\/code>&nbsp;returned two lines. The line you were looking for,&nbsp;<strong>Port 22<\/strong>, and an additional line containing the search pattern. In some cases, that&#8217;s exactly what you want. In other cases,&nbsp;<code>grep<\/code>&nbsp;could find too many entries that you&#8217;re not interested in, requiring you to sort through them to find the desired information.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To avoid that, you can use regular expressions to be more specific about what you&#8217;re looking for. For example, to find only lines that start with the word&nbsp;<code>Port<\/code>, you can use the regular expression operator&nbsp;<code>^<\/code>, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ grep ^Port \/etc\/ssh\/sshd_config\nPort 22<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This time&nbsp;<code>grep<\/code>&nbsp;returned only the line that started with&nbsp;<code>Port<\/code>&nbsp;since, in the second line, the expression&nbsp;<code>Port<\/code>&nbsp;is in the middle.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also use&nbsp;<em>extended<\/em>&nbsp;regular expressions with the command-line parameter&nbsp;<code>-E<\/code>. For example, to search for a pattern that contains the word&nbsp;<code>Port<\/code>&nbsp;followed by numbers, use this regular expression:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ grep -E \"Port &#91;1-9]+\" \/etc\/ssh\/sshd_config\nPort 22<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can also look for lines that end with a text pattern by using the&nbsp;<code>$<\/code>&nbsp;operator. For example, to find all lines that end with&nbsp;<code>none<\/code>&nbsp;in&nbsp;<code>sshd_config<\/code>, use&nbsp;<code>grep<\/code>&nbsp;like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ grep none$ \/etc\/ssh\/sshd_config\n<em>#RekeyLimit default none<\/em>\n<em>#AuthorizedPrincipalsFile none<\/em>\n<em>#AuthorizedKeysCommand none<\/em>\n<em>#ChrootDirectory none<\/em>\n<em>#VersionAddendum none<\/em>\n<em>#Banner none<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Regular expressions are a big part of&nbsp;<code>grep<\/code>, making it powerful and flexible. However, regular expressions are a huge topic. For additional information, look at&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Regular_expression\" target=\"_blank\" rel=\"noreferrer noopener\">Regular expression on Wikipedia<\/a>&nbsp;or&nbsp;<a href=\"https:\/\/regex101.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Regular expressions 101<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Find text in multiple files and directories<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Similar to finding text patterns in a single file, you can use&nbsp;<code>grep<\/code>&nbsp;to find text in multiple files or directories. To find text in multiple files simultaneously, specify which files to search from after the first file name, or use a shell wildcard such as&nbsp;<code>*<\/code>&nbsp;for all files. For example, to search for a configuration in two files:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ grep Port \/etc\/ssh\/sshd_config \/etc\/ssh\/ssh_config\n\/etc\/ssh\/sshd_config:Port 22\n\/etc\/ssh\/sshd_config:<em>#GatewayPorts no<\/em>\n\/etc\/ssh\/ssh_config:<em>#   Port 22<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When you use multiple files,&nbsp;<code>grep<\/code>&nbsp;shows the name of the file where it found a match before showing the matched line.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>[ Keep your most commonly used commands handy with the&nbsp;<a href=\"https:\/\/developers.redhat.com\/cheat-sheets\/linux-commands-cheat-sheet?intcmp=701f20000012ngPAAQ\" target=\"_blank\" rel=\"noreferrer noopener\">Linux commands cheat sheet<\/a>. ]<\/em><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To run the search recursively in multiple subdirectories, use the command line flag&nbsp;<code>-R<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ grep -R ^Port \/etc\n\/etc\/ssh\/sshd_config:Port 22<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>grep<\/code>&nbsp;command is fast and returns results quickly, but it may take a long time if you specify too many files or subdirectories to search.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Find text in another command&#8217;s output<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Similar to other Unix utilities,&nbsp;<code>grep<\/code>&nbsp;also acts on&nbsp;<code>stdin<\/code>&nbsp;when you pipe the output of another command into it. This is a fast and useful way to filter a command&#8217;s output to match the text pattern you&#8217;re looking for.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, if you want to check whether the package&nbsp;<code>openssh<\/code>&nbsp;is installed in your Fedora or&nbsp;<a href=\"https:\/\/www.redhat.com\/en\/technologies\/linux-platforms\/enterprise-linux?intcmp=701f20000012ngPAAQ\" target=\"_blank\" rel=\"noreferrer noopener\">Red Hat Enterprise Linux (RHEL)<\/a>&nbsp;operating system, you can pipe the output of command&nbsp;<code>rpm -qa<\/code>, which lists all installed packages, into&nbsp;<code>grep<\/code>&nbsp;to search for the pattern:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ rpm -qa | grep ssh\nlibssh-config-0.9.6-4.fc36.noarch\nlibssh-0.9.6-4.fc36.x86_64\nopenssh-8.8p1-1.fc36.1.x86_64<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can filter long command outputs with&nbsp;<code>grep<\/code>, making finding useful information easier.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>[ Get the guide to&nbsp;<a href=\"https:\/\/opensource.com\/downloads\/installing-linux-applications-ebook?intcmp=701f20000012ngPAAQ\" target=\"_blank\" rel=\"noreferrer noopener\">installing applications on Linux<\/a>. ]<\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Additional useful options<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>grep<\/code>&nbsp;command provides many options to change how it searches for patterns or displays results. So far in this article, you&#8217;ve seen some of them. While I can&#8217;t list all options, here are some other useful examples:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.redhat.com\/sysadmin\/find-text-files-using-grep#bottom-list\">Skip to the bottom of list<\/a>Great Linux resources<ul><li><a href=\"https:\/\/developers.redhat.com\/cheat-sheets\/advanced-linux-commands\/?intcmp=701f20000012ngPAAQ\">Advanced Linux commands cheat sheet<\/a><\/li><li><a href=\"https:\/\/developers.redhat.com\/products\/rhel\/download?intcmp=701f20000012ngPAAQ\">Download RHEL 9 at no charge through the Red Hat Developer program<\/a><\/li><li><a href=\"https:\/\/opensource.com\/downloads\/installing-linux-applications-ebook?intcmp=701f20000012ngPAAQ\">A guide to installing applications on Linux<\/a><\/li><li><a href=\"https:\/\/rhtapps.redhat.com\/assessment\/?intcmp=701f20000012ngPAAQ\">Linux system administration skills assessment<\/a><\/li><li><a href=\"https:\/\/redhatdg.co1.qualtrics.com\/jfe\/form\/SV_bjRFSHqPdTpIjoa?intcmp=701f20000012ngPAAQ\">How well do you know Linux? Take a quiz and get a badge<\/a><\/li><\/ul>Use option&nbsp;<code>-i<\/code>&nbsp;for a case-insensitive search.<\/li>\n\n\n\n<li>Use option&nbsp;<code>-v<\/code>&nbsp;to invert the search and display&nbsp;lines that do not match the pattern.<\/li>\n\n\n\n<li>Use option&nbsp;<code>-w<\/code>&nbsp;to search for entire words only&nbsp;instead of patterns in the middle of other words.<\/li>\n\n\n\n<li>Use option&nbsp;<code>--color<\/code>&nbsp;for colored output, making it easier to spot the matched pattern.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For a complete list of&nbsp;<code>grep<\/code>&nbsp;options, consult the man pages.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The GNU grep utility is flexible and useful, helping you accomplish many tasks in your daily sysadmin activities. The more you use&nbsp;<code>grep<\/code>, the more comfortable you will become, and soon you&#8217;ll notice you&#8217;re relying on it all the time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using grep, you can quickly find text matching a regular expression in a single file, a group of files, or text coming from stdin. The most common way to find text in a Linux system is using the command-line utility&nbsp;grep. This utility was originally developed for the Unix operating system in the early 1970s. Grep &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-55","post","type-post","status-publish","format-standard","hentry","category-linux","category-pi"],"_links":{"self":[{"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=55"}],"version-history":[{"count":0,"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/55\/revisions"}],"wp:attachment":[{"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}