{"id":15644,"date":"2019-02-25T12:32:24","date_gmt":"2019-02-25T12:32:24","guid":{"rendered":"https:\/\/www.hostinger.com\/tutorials\/?p=15644"},"modified":"2025-04-28T07:54:55","modified_gmt":"2025-04-28T07:54:55","slug":"grep-command-in-linux","status":"publish","type":"post","link":"\/uk\/tutorials\/grep-command-in-linux","title":{"rendered":"How to use the grep command in Linux + examples\u00a0"},"content":{"rendered":"<p>The <strong>Global Regular Expression Print<\/strong> or <strong>grep<\/strong> command searches specific lines containing a particular pattern in a file. It is useful for quickly checking if an entry exists, especially for large items like log files.<\/p><p>You can add various options to modify the command behavior for specific tasks, like inverse search and multi-keyword lookup. Due to its flexibility, grep is an essential tool for managing your Linux virtual private server (VPS).<\/p><p>This article will explain how to use the grep command in Linux, including its syntax and options. You will also learn some of its real-world applications for managing files in your system.&nbsp;<\/p><p>\n\n\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-grep-command-syntax\">Grep command syntax<\/h2><p>The basic grep syntax looks as follows:<\/p><pre class=\"wp-block-preformatted\">grep [options] pattern [FILE]&nbsp;<\/pre><p>Here is the explanation of what each element means:<\/p><ul class=\"wp-block-list\">\n<li><strong>grep<\/strong> &ndash; the utility name.<\/li>\n\n\n\n<li><strong>options<\/strong> &ndash; arguments that modify the grep command&rsquo;s behavior.<\/li>\n\n\n\n<li><strong>pattern<\/strong> &ndash; the keyword you want to look up.<\/li>\n\n\n\n<li><strong>file<\/strong> &ndash; the item whose content you want to search.&nbsp;<\/li>\n<\/ul><p>In the later section, we will explain the most commonly used options and their real-world usage examples.<\/p><p>Read the <a href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/grep.1.html\" target=\"_blank\" rel=\"noopener\">grep manual<\/a> to learn more about all the options. Alternatively, check this utility guide in your command-line interface by entering:<\/p><pre class=\"wp-block-preformatted\">grep --help<\/pre><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e00a8a7342f\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"396\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/08\/grep-command-guide-in-terminal-1024x396.png\" alt=\"grep command guide in Terminal\" class=\"wp-image-113751\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-command-guide-in-terminal-1024x396.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-command-guide-in-terminal-300x116.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-command-guide-in-terminal-150x58.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-command-guide-in-terminal-768x297.png 768w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-command-guide-in-terminal.png 1360w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>Remember that you must have the &ldquo;read&rdquo; privilege of the file you want to check with grep. Read our tutorial on <a href=\"\/uk\/tutorials\/how-to-change-linux-permissions-and-owners\">how to change permissions in Linux<\/a>.<\/p><h2 class=\"wp-block-heading\" id=\"h-using-the-linux-grep-command\">Using the Linux grep command<\/h2><p>In this section, we will discuss common usage of the grep utility. If you are using a VPS, run the command via an <a href=\"\/uk\/tutorials\/how-to-use-putty-ssh\">SSH client like PuTTY<\/a>. For desktops, open the Linux Terminal by pressing <strong>Ctrl + Alt + T<\/strong>.<\/p><h3 class=\"wp-block-heading\" id=\"h-basic-search\">Basic search<\/h3><p>The most basic usage of grep is to filter text files, outputting lines that contain a particular word. To do so, run this command without an option like this syntax:<\/p><pre class=\"wp-block-preformatted\">grep keyword file<\/pre><p>You can also list multiple file names like so:<\/p><pre class=\"wp-block-preformatted\">grep pattern file1 file2 file3<\/pre><p>If you want to use a phrase as the search string, enclose the pattern using a quotation mark. For example, this command will find lines containing <strong>Linux distro <\/strong>in the<strong> hostinger.txt <\/strong>sample file:<\/p><pre class=\"wp-block-preformatted\">grep \"Linux distro\" hostinger.txt&nbsp;<\/pre><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e00a8a74d75\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"103\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/08\/grep-outputs-matching-lines-from-a-file-1024x103.png\" alt=\"grep outputs matching lines from a file\" class=\"wp-image-113752\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-outputs-matching-lines-from-a-file-1024x103.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-outputs-matching-lines-from-a-file-300x30.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-outputs-matching-lines-from-a-file-150x15.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-outputs-matching-lines-from-a-file-768x78.png 768w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-outputs-matching-lines-from-a-file.png 1128w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>Note that the syntax only works if the input file is within your current directory. If it is in another location, specify the full path like so:<\/p><pre class=\"wp-block-preformatted\">grep pattern \/path\/to\/the\/file.txt<\/pre><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> Since Linux commands like grep are case-sensitive, pay attention to capitalization in your search pattern and file name.<\/p><\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-recursive-search\">Recursive search<\/h3><p>You can recursively search all the files and subdirectories within a folder using the <strong>-r<\/strong> option. Here is the syntax:<\/p><pre class=\"wp-block-preformatted\">grep -r keyword path\/to\/a\/folder<\/pre><p>For example, we have a <strong>tutorial\/content\/linux <\/strong>directory containing two folders called <strong>commands<\/strong> and<strong> utilities<\/strong>. To search the word <strong>VPS<\/strong> in all files within these folders, simply run grep on the parent path:<\/p><pre class=\"wp-block-preformatted\">grep -r VPS tutorial\/content\/linux<\/pre><p>The grep command will list matched lines from any file and folder within the <strong>tutorial\/content\/linux <\/strong>directory.<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e00a8a76934\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"96\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/08\/grep-recursively-searches-files-within-different-subfolders-1024x96.png\" alt=\"grep recursively searches files within different subfolders\" class=\"wp-image-113753\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-recursively-searches-files-within-different-subfolders-1024x96.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-recursively-searches-files-within-different-subfolders-300x28.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-recursively-searches-files-within-different-subfolders-150x14.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-recursively-searches-files-within-different-subfolders-768x72.png 768w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-recursively-searches-files-within-different-subfolders.png 1274w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-print-entries-around-the-matching-line\">Print entries around the matching line<\/h3><p>Grep lets you output lines after and before the matching entry to provide you with more context. To do so, add these options followed by the number of neighboring lines you want to print:<\/p><ul class=\"wp-block-list\">\n<li><strong>A<\/strong> &ndash; prints the line after the matching entry.<\/li>\n\n\n\n<li><strong>B<\/strong> &ndash; outputs the line before the matching entry.<\/li>\n\n\n\n<li><strong>C<\/strong> &ndash; displays lines both after and before the entry.<\/li>\n<\/ul><p>For example, run the following grep syntax to print an entry after the matching lines:<\/p><pre class=\"wp-block-preformatted\">grep -A1 pattern file<\/pre><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e00a8a77e46\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"92\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/08\/grep-prints-a-line-after-the-matching-entry-1024x92.png\" alt=\"grep prints a line after the matching entry\" class=\"wp-image-113754\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-prints-a-line-after-the-matching-entry-1024x92.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-prints-a-line-after-the-matching-entry-300x27.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-prints-a-line-after-the-matching-entry-150x13.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-prints-a-line-after-the-matching-entry-768x69.png 768w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-prints-a-line-after-the-matching-entry.png 1048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-case-insensitive-search\">Case-insensitive search<\/h3><p>By default, grep will treat patterns with different letter cases as a different string. For example, you can&rsquo;t search <strong>Grep<\/strong> and<strong> GREP<\/strong> using the keyword <strong>grep<\/strong>. You can disable this behavior by adding the <strong>-i<\/strong> option:<\/p><pre class=\"wp-block-preformatted\">grep -i keyword file<\/pre><p>If we want to search for the word <strong>vps<\/strong> and ignore case distinctions, run the following command:<\/p><pre class=\"wp-block-preformatted\">grep -i vps hostinger.txt<\/pre><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e00a8a79355\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"74\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/08\/grep-finds-the-pattern-vps-ignoring-case-distinctions-1024x74.png\" alt=\"Grep finds the pattern VPS ignoring case distinctions\" class=\"wp-image-113755\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-finds-the-pattern-vps-ignoring-case-distinctions-1024x74.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-finds-the-pattern-vps-ignoring-case-distinctions-300x22.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-finds-the-pattern-vps-ignoring-case-distinctions-150x11.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-finds-the-pattern-vps-ignoring-case-distinctions-768x56.png 768w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-finds-the-pattern-vps-ignoring-case-distinctions.png 1134w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-inverted-search\">Inverted search<\/h3><p>Add the <strong>-v<\/strong> option to your grep command like this syntax to enable the reverse lookup, printing non-matching lines:<\/p><pre class=\"wp-block-preformatted\">grep -v keyword file<\/pre><p>For example, this command displays only those lines without the word VPS from the <strong>hostinger.txt<\/strong> file:<\/p><pre class=\"wp-block-preformatted\">grep -v VPS hostinger.txt<\/pre><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e00a8a7a797\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" width=\"910\" height=\"90\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/08\/grep-prints-lines-that-dont-match-the-searched-pattern.png\" alt=\"Grep prints lines that don't match the searched pattern\" class=\"wp-image-113756\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-prints-lines-that-dont-match-the-searched-pattern.png 910w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-prints-lines-that-dont-match-the-searched-pattern-300x30.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-prints-lines-that-dont-match-the-searched-pattern-150x15.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-prints-lines-that-dont-match-the-searched-pattern-768x76.png 768w\" sizes=\"(max-width: 910px) 100vw, 910px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-display-line-numbers\">Display line numbers<\/h3><p>If you are working with large files like a log, the output data can be very long. It makes checking the location of the pattern difficult. To simplify the task, you can display the line numbers with the <strong>-n<\/strong> option like so:<\/p><pre class=\"wp-block-preformatted\">grep -n keyword file<\/pre><p>For example, this command will print the lines from <strong>auth.log<\/strong> containing the word <strong>unknown user<\/strong> and their numbers:<\/p><pre class=\"wp-block-preformatted\">grep -n \"unknown user\" auth.log<\/pre><p>The line that begins with the number one means it is the first entry from the <strong>auth.log<\/strong> file, and so on.<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e00a8a7c29d\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" width=\"1286\" height=\"196\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/08\/grep-command-shows-the-line-numbers.png\" alt=\"Grep command shows the line numbers\" class=\"wp-image-113758\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-command-shows-the-line-numbers.png 1286w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-command-shows-the-line-numbers-300x46.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-command-shows-the-line-numbers-1024x156.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-command-shows-the-line-numbers-150x23.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-command-shows-the-line-numbers-768x117.png 768w\" sizes=\"(max-width: 1286px) 100vw, 1286px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p> You can combine grep options to refine the search. For example, run <strong>grep -nv pattern file<\/strong> to display the line number of entries that dont contain the specified pattern.<\/p>\n                <\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-using-grep-with-regular-expressions\">Using grep with regular expressions<\/h3><p><a href=\"https:\/\/www.regular-expressions.info\/\" target=\"_blank\" rel=\"noopener\">Regular expressions<\/a>, or regex, are strings of characters that define a search pattern. It is useful for finding a very specific query without having to list the keywords individually. Here are several regex symbols and their signifiers:<\/p><ul class=\"wp-block-list\">\n<li><strong>.<\/strong> &ndash; matches any single character.<\/li>\n\n\n\n<li><strong>*<\/strong> &ndash; represents zero or more of the preceding character.<\/li>\n\n\n\n<li><strong>+<\/strong> &ndash; signifies one or more of the preceding characters.<\/li>\n\n\n\n<li><strong>?<\/strong> &ndash; indicates zero or one of the preceding characters.<\/li>\n\n\n\n<li><strong>^<\/strong> &ndash; indicates the start of a regex pattern.<\/li>\n\n\n\n<li><strong>$<\/strong> &ndash; represents the end of a regex pattern.<\/li>\n\n\n\n<li><strong>()<\/strong> &ndash; groups patterns.<\/li>\n<\/ul><p>These regular expressions behave differently whether you use the <strong>extended regular expression <\/strong>(ERE) with the <strong>-E<\/strong> option or the default <strong>basic regular expression<\/strong> (BRE). For more information, read about the<a href=\"https:\/\/www.gnu.org\/software\/grep\/manual\/grep.html#Basic-vs-Extended\" target=\"_blank\" rel=\"noopener\"> differences between BRE and ERE<\/a>.<\/p><p>For example, if you want to search patterns like <strong>abc <\/strong>and<strong> acc<\/strong>, run the following command. The dot (<strong>.<\/strong>) symbol will match any character:<\/p><pre class=\"wp-block-preformatted\">grep -E 'a.c' file<\/pre><p>The <strong>^<\/strong> and <strong>$<\/strong> symbols let you search for lines that begin or end with a specific pattern. Consider this grep command example:<\/p><pre class=\"wp-block-preformatted\">grep -E '^pattern' file<\/pre><p>The above command will match lines that begin with <strong>pattern<\/strong>, like <strong>pattern is a string of text<\/strong>. Meanwhile, the following will search for entries that end with<strong> pattern<\/strong>:<\/p><pre class=\"wp-block-preformatted\">grep -E 'pattern$' file<\/pre><p>You can also combine multiple regex symbols to refine your search for a more specific result. For example, this command will look for lines containing any word that begins with an <strong>H<\/strong> and ends with <strong>R<\/strong>:<\/p><pre class=\"wp-block-preformatted\">grep '^H.*R$' 1.txt<\/pre><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e00a8a7d83c\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" width=\"668\" height=\"116\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/08\/grep-searches-patterns-using-regex-1.png\" alt=\"Grep searches patterns using regex\" class=\"wp-image-113759\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-searches-patterns-using-regex-1.png 668w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-searches-patterns-using-regex-1-300x52.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-searches-patterns-using-regex-1-150x26.png 150w\" sizes=\"(max-width: 668px) 100vw, 668px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-search-multiple-patterns\">Search multiple patterns<\/h3><p>You can search multiple keywords using a single grep command. The simplest way to do so is by listing the keywords within quotation marks (<strong>&lsquo;<\/strong>) separated by backslashes (<strong>\\<\/strong>) and pipes (<strong>|<\/strong>). Here is how the syntax looks:<\/p><pre class=\"wp-block-preformatted\">grep 'pattern1\\|pattern2\\|pattern3' file<\/pre><p>In this command, grep uses BRE since we are not adding any options. To prevent it from interpreting the pipe as the queried pattern, we escape it using the backslash.<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e00a8a7eb9e\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"70\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/08\/grep-searches-two-patterns-from-a-file-1024x70.png\" alt=\"Grep searches two patterns from a file\" class=\"wp-image-113760\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-searches-two-patterns-from-a-file-1024x70.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-searches-two-patterns-from-a-file-300x20.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-searches-two-patterns-from-a-file-150x10.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-searches-two-patterns-from-a-file-768x52.png 768w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-searches-two-patterns-from-a-file.png 1260w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>You can also search multiple patterns without a backslash by adding the <strong>-E<\/strong> option to enable the pipe&rsquo;s special function like so:<\/p><pre class=\"wp-block-preformatted\">grep -E 'pattern1|pattern2|pattern3' file<\/pre><p>Instead of the -E option, you can do so using the <strong>egrep<\/strong> command:<\/p><pre class=\"wp-block-preformatted\">egrep 'pattern1|pattern2|pattern3' file<\/pre><p>If you want to specify the patterns separately, use the <strong>-e<\/strong> option for each search query. Here is the command syntax:<\/p><pre class=\"wp-block-preformatted\">grep -e pattern1 -e pattern2 -e pattern3 file<\/pre><p>All these grep command variations output the same result. So, choose one based on your preferences.<\/p><h3 class=\"wp-block-heading\" id=\"h-count-matches\">Count matches<\/h3><p>Several system administration tasks might require you to check how many lines in a file have a matching pattern. For example, this is useful for counting login attempts from a specific IP address in a log file.<\/p><p>To check how many lines contain a pattern, use the <strong>-c<\/strong> or <strong>&ndash;count<\/strong> option like so:<\/p><pre class=\"wp-block-preformatted\">grep -c pattern file<\/pre><p>However, the <strong>-c<\/strong> option counts the matching lines, not the pattern occurrence. If you want to check it, use the <strong>-o<\/strong> option and pass it to the <strong>wc<\/strong> command like so:<\/p><pre class=\"wp-block-preformatted\">grep -o pattern file | wc -l<\/pre><p>Instead of displaying the entire line, the <strong>-o<\/strong> option prints only the pattern occurrences, which the wc command count. We will discuss using a pipe (<strong>|<\/strong>) in the later section.<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e00a8a8030f\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"60\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/08\/grep-counts-the-number-of-pattern-occurences-1024x60.png\" alt=\"grep counts the number of pattern occurrences\" class=\"wp-image-113761\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-counts-the-number-of-pattern-occurences-1024x60.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-counts-the-number-of-pattern-occurences-300x18.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-counts-the-number-of-pattern-occurences-150x9.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-counts-the-number-of-pattern-occurences-768x45.png 768w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/08\/grep-counts-the-number-of-pattern-occurences.png 1092w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>Note that this command also counts other variants of the keyword. For example, if you use the pattern <strong>connect<\/strong>, grep will check for the likes of <strong>reconnect<\/strong>, <strong>connected<\/strong>, <strong>connecting<\/strong>, and <strong>disconnected<\/strong>.<\/p><p>If you want to count the exact matching word, add the word boundaries at the beginning and the end of your keyword. Here&rsquo;s an example:<\/p><pre class=\"wp-block-preformatted\">grep -o '\\bpattern\\b' file&nbsp;<\/pre><h3 class=\"wp-block-heading\" id=\"h-combine-grep-with-other-commands\">Combine grep with other commands<\/h3><p>In addition to files and directories, you can filter input data from another command using grep. To do so, combine them using a pipe (<strong>|<\/strong>) like so:<\/p><pre class=\"wp-block-preformatted\">command | grep pattern<\/pre><p>In the snippet, the pipe redirects the output from the first command to grep, filtering the result based on the keyword. Here is a real-world usage example:<\/p><pre class=\"wp-block-preformatted\">ls | grep '\\.txt$'<\/pre><p>The <strong>ls<\/strong> command lists the content of your current directory. Then, the<strong> grep<\/strong> command filters the output, printing <strong>TXT<\/strong> files within the folder. Here is another example:<\/p><pre class=\"wp-block-preformatted\">ps aux | grep \"process_name\"&nbsp;<\/pre><p>The <strong>ps aux<\/strong> command lists all running processes in your system. Then, the grep utility searches for a particular one from the list based on its name.<\/p><p>Depending on your task, you can combine grep with other tools. <a href=\"\/uk\/vps-hosting\">Hostinger VPS hosting<\/a> users who are unsure which utilities to use can use our <a href=\"\/uk\/blog\/vps-ai-assistant\">AI assistant, <strong>Kodee<\/strong><\/a>, to help explain and generate the complete command.<\/p><p>For example, ask <strong>Kodee<\/strong>, &ldquo;I want to list all services in my Ubuntu VPS and use the grep utility to filter the output, printing only ones related to NGINX. Generate the full command.&rdquo; <\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e00a8a81aa1\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"502\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/10\/grep-command-generated-by-kodee-1024x502.png\" alt=\"grep command generated by Kodee AI assistant\" class=\"wp-image-117107\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/10\/grep-command-generated-by-kodee-1024x502.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/10\/grep-command-generated-by-kodee-300x147.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/10\/grep-command-generated-by-kodee-150x73.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/10\/grep-command-generated-by-kodee-768x376.png 768w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/10\/grep-command-generated-by-kodee-1536x752.png 1536w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/10\/grep-command-generated-by-kodee-2048x1003.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>It will then write the appropriate command for your system and needs.<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/uk\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner-1024x300.png\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-300x88.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-150x44.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-768x225.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h3 class=\"wp-block-heading\" id=\"h-redirect-output\">Redirect output<\/h3><p>System administrators might need to save the grep command results for further processing or an archive. An easy way to do so is by redirecting the output to a text file using the greater than (<strong>&gt;<\/strong>) symbol. Here is the syntax:<\/p><pre class=\"wp-block-preformatted\">grep pattern file &gt; target_file.txt<\/pre><p>If the target file doesn&rsquo;t exist, the grep command will automatically create it in your current working directory.<\/p><p>To check if grep saves the output correctly, print the content of the target file using the <strong>cat <\/strong>command like so:<\/p><pre class=\"wp-block-preformatted\">cat target_file.txt<\/pre><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Want to learn more about Linux Commands?<\/h4>\n                    <p> Check out our <a href=\"\/uk\/tutorials\/linux-commands\">Linux commands<\/a> tutorial to learn more about other commonly used server management utilities.<\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>The <strong>grep <\/strong>command lets you use a pattern to find lines from a file. Its basic syntax is <strong>grep pattern file<\/strong>, but you can add various options to modify the search based on your needs.<\/p><p>For example, add the <strong>-r <\/strong>option to recursively search patterns from files and subdirectories within a folder. While grep is case-sensitive by default, you can disable this setting using the -i flag to find a matched pattern regardless of its capitalization.<\/p><p>You can also print non-matching lines using the <strong>-v<\/strong> option and add the <strong>-n<\/strong> option to show their sequence number. For more complex search patterns, you can use regular expression symbols.<\/p><p>In real world usage, you might need to combine grep with other commands and redirect the search results to a file for backup. Since this utility is very versatile, use <strong>Kodee<\/strong> to write the full command.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-use-the-grep-command-faq\">How to use the grep command FAQ<\/h2><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1722508228700\"><h3 class=\"schema-faq-question\">What is the grep command used for?<\/h3> <p class=\"schema-faq-answer\">The grep command is commonly used to search lines containing a specific keyword in a file. It is also useful for filtering another utility&rsquo;s output, printing only ones with a particular pattern. You can do so by piping the command to grep.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1722508234937\"><h3 class=\"schema-faq-question\">What are the most common options used with grep?<\/h3> <p class=\"schema-faq-answer\">One of the most common options in grep includes <strong>-i<\/strong>, which ignores the case sensitivity in your search query. Another popular option is <strong>-r<\/strong>, enabling you to search all files and folders within a directory.&nbsp;<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1722508239000\"><h3 class=\"schema-faq-question\">Can I search for a pattern in multiple files or directories using grep?<\/h3> <p class=\"schema-faq-answer\">Yes, you can search a keyword in multiple files or directories by listing them. Enter <strong>grep pattern file1.txt file2.txt <\/strong>to find a pattern in files. To check keywords from multiple directories, use the <strong>-r <\/strong>option followed by their parent&rsquo;s path.&nbsp;<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1722508246033\"><h3 class=\"schema-faq-question\">How do I search for lines that do not match a pattern?<\/h3> <p class=\"schema-faq-answer\">To print lines that don&rsquo;t match a pattern, add the <strong>-v<\/strong> option to your grep command. It will enable the reverse search functionality, outputting all entries from a file that doesn&rsquo;t contain the keyword.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Global Regular Expression Print or grep command searches specific lines containing a particular pattern in a file. It is useful for quickly checking if an entry exists, especially for large items like log files. You can add various options to modify the command behavior for specific tasks, like inverse search and multi-keyword lookup. Due [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/uk\/tutorials\/grep-command-in-linux\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":337,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"Using the Linux grep command","rank_math_description":"The grep command in Linux searches text for specific patterns and displays matching lines. Read this article to learn how to use this command.","rank_math_focus_keyword":"grep command","footnotes":""},"categories":[22644,22640],"tags":[],"class_list":["post-15644","post","type-post","status-publish","format-standard","hentry","category-managing-monitoring-and-security","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/grep-command-in-linux","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/commande-grep-sous-linux","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/comando-grep-linux","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/grep-command","default":0},{"locale":"nl-NL","link":"https:\/\/www.hostinger.com\/nl\/tutorials\/grep-command","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/grep-command-in-linux","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/grep-command-in-linux","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/grep-command-in-linux","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/comando-grep-linux","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/comando-grep-linux","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/comando-grep-linux","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/grep-command-in-linux","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/grep-command-in-linux","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/grep-command-in-linux","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/grep-command-in-linux","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/15644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/users\/337"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/comments?post=15644"}],"version-history":[{"count":29,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/15644\/revisions"}],"predecessor-version":[{"id":121449,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/15644\/revisions\/121449"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/media?parent=15644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/categories?post=15644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/tags?post=15644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}