{"id":47941,"date":"2025-01-09T08:07:36","date_gmt":"2025-01-09T08:07:36","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=47941"},"modified":"2025-01-09T08:07:36","modified_gmt":"2025-01-09T08:07:36","slug":"search-files-given-text-linux-command","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2025\/01\/search-files-given-text-linux-command\/","title":{"rendered":"Search Which Files include Given Text\/String in Linux (Examples)"},"content":{"rendered":"<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-37909\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo-250x250.webp\" alt=\"\" width=\"250\" height=\"250\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo-250x250.webp 250w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo-300x300.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo-600x600.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo-768x768.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo.webp 1200w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p>This tutorial shows how to search and print which files contain your specified text or string in Ubuntu and other Linux in command line, with examples.<\/p>\n<p>Search file or file content is an important skill for Linux administrator. I&#8217;ve shown how to <a href=\"https:\/\/ubuntuhandbook.org\/index.php\/2024\/03\/locate-command-examples\/\" target=\"_blank\" rel=\"noopener\">use locate command to search files<\/a> through the keyword in its file-name, path, and file type.<\/p>\n<p>Here I&#8217;m going to show you how to search files if you know any text or string they contain.<\/p>\n<p><!--more--><\/p>\n<h3>Method 1: Use grep command<\/h3>\n<p><code>grep<\/code> is a very popular command line tool that most Linux includes it out-of-the-box. I use it quite often to filter content in command output.<\/p>\n<p>In my case, building an app sometimes work in recent Ubuntu but NOT for older Ubuntu. It outputs error &#8220;has no member or class named &#8216;xxxxxx'&#8221;. I need to find out which file contains that member\/class in recent Ubuntu and backport to older Ubuntu version.<\/p>\n<p>1. In the case, I can run the command below, for example, to <b>search files under <code>\/usr\/include<\/code> directory which include string <code>GParamSpecClass<\/code> in file content<\/b>.<\/p>\n<pre>grep -lr \"GParamSpecClass\" \/usr\/include\/<\/pre>\n<p>Here you may replace the string and searching directory accordingly, or skip\u00a0<code>\/usr\/include\/<\/code> to search under current working directory.<\/p>\n<p>And, the command options are:<\/p>\n<ul>\n<li><code>-l<\/code> or <code>--files-with-matches<\/code> means only print names of files containing matches.<\/li>\n<li><code>-r<\/code> or <code>--recursive<\/code>, also search sub-folders for given directory.<\/li>\n<\/ul>\n<p>As you know, Linux is case sensitive. To ignore, use <code>-i<\/code> or <code>--ignore-case<\/code> in command. So, the commands below work same as last:<\/p>\n<pre>grep -lir \"gparamspecclass\" \/usr\/include\/<\/pre>\n<pre>grep -lir \"GPARAMSPECclass\" \/usr\/include\/<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-listfiles-match.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47943\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-listfiles-match-700x269.webp\" alt=\"\" width=\"610\" height=\"234\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-listfiles-match-700x269.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-listfiles-match-300x115.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-listfiles-match.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>2. For choice, you may <b>print names of files, matched lines, as well as line numbers<\/b> of files contain &#8220;GParamSpecClass&#8221; under \/usr\/include\/, by running similar command below:<\/p>\n<pre>grep -irn \"GParamSpecClass\" \/usr\/include\/<\/pre>\n<p>As you see, in command:<\/p>\n<ul>\n<li>Skip <code>-l<\/code> or <code>--files-with-matches<\/code>, then it prints matched lines in output.<\/li>\n<li>Add <code>-n<\/code> or <code>--line-number<\/code>, will also show the line numbers.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-listfiles-lines.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47944\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-listfiles-lines-700x381.webp\" alt=\"\" width=\"610\" height=\"332\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-listfiles-lines-700x381.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-listfiles-lines-300x163.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-listfiles-lines-768x418.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-listfiles-lines.webp 770w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>3. To show more file content, you may add:<\/p>\n<ul>\n<li><code>-A NUM<\/code> or <code>--after-context=NUM<\/code> to print NUM lines of trailing context after matching lines.<\/li>\n<li><code>-B NUM<\/code> or <code>--before-context=NUM<\/code> to print NUM lines of leading context before matching lines<\/li>\n<\/ul>\n<p>For example, search files under <code>\/usr\/include<\/code> directory that include &#8220;<code>g_cclosure_marshal_generic<\/code>&#8221; string, print files names, matched lines, as well as 2 lines before and 4 lines after matches.<\/p>\n<pre>grep -irn -B 2 -A 4 \"g_cclosure_marshal_generic\" \/usr\/include\/<\/pre>\n<p>Sometimes, there may be multiple files or multiple lines match what you search. In the case, you may add <code>-m NUM<\/code> or <code>--max-count=NUM<\/code> to search first NUM matches.<\/p>\n<p>For example, the command below will do same as the last, but only show the first one that matches.<\/p>\n<pre>grep -irn -m 1 -B 2 -A 4 \"g_cclosure_marshal_generic\" \/usr\/include\/<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-beforeafter-first.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47945\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-beforeafter-first-700x502.webp\" alt=\"\" width=\"610\" height=\"437\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-beforeafter-first-700x502.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-beforeafter-first-300x215.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-beforeafter-first-768x551.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/grep-beforeafter-first.webp 946w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><code>grep<\/code> supports regex expressions to make searching more flexible. Instead of typing the exact string or text you want to search, you may use &#8220;<code>^string<\/code>&#8221; to search files contain <code>string<\/code> at beginning of lines, use &#8220;<code>string$<\/code>&#8221; to search files contain the string in end of lines, or <code>stringA|stringB<\/code> to search files contain either <code>stringA<\/code> or <code>stringB<\/code>.<\/p>\n<p>They are so many regex patterns that I cannot include them all in this tutorial, see <a href=\"https:\/\/en.wikipedia.org\/wiki\/Regular_expression\" target=\"_blank\" rel=\"noopener\">wikipedia page<\/a> for more.<\/p>\n<h3>Method 2: Use ag command<\/h3>\n<p>It&#8217;s hard for me to remember infrequently used command options. In the case, I tend to use alternatives that just works with default arguments. <code>ag<\/code> is the one for searching file content.<\/p>\n<p>1. First, <b>run command to install ag<\/b>.<\/p>\n<ul>\n<li>For Debian, Ubuntu, Linux Mint, use command:\n<pre>sudo apt install silversearcher-ag<\/pre>\n<\/li>\n<li>For Fedora, RHEL, etc, run command:\n<pre>sudo dnf install the_silver_searcher<\/pre>\n<\/li>\n<li>While, Arch based systems may use the command below instead:\n<pre>sudo pacman -S the_silver_searcher<\/pre>\n<\/li>\n<\/ul>\n<p>2. Then, for example <b>search files under <code>\/usr\/include<\/code> that include string &#8220;<code>gparamspecclass<\/code>&#8220;<\/b>:<\/p>\n<pre>ag \"gparamspecclass\" \/usr\/include<\/pre>\n<p>As the screenshot shows, it prints all the names of files, lines and line numbers that matches. And, it&#8217;s case insensitive.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/ag-search.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47946\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/ag-search-700x378.webp\" alt=\"\" width=\"610\" height=\"329\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/ag-search-700x378.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/ag-search-300x162.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/ag-search.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><code>ag<\/code> command is super fast! In case the default options do not meet your needs, here are some that similar to <code>grep<\/code>:<\/p>\n<ul>\n<li><code>-A NUM<\/code> or <code>--after NUM<\/code>, print NUM lines after matches.<\/li>\n<li><code>-B NUM<\/code> or <code>--before NUM<\/code>, print NUM lines before matches.<\/li>\n<li><code>-c<\/code> or <code>--count<\/code>, print only names of files and the number of matches.<\/li>\n<li><code>-m NUM<\/code> or <code>--max-count NUM<\/code>, print only first NUM matches.<\/li>\n<li><code>-f<\/code> or <code>--follow<\/code>, also search files link to others out of searching directory.<\/li>\n<li><code>-l<\/code> or <code>--files-with-matches<\/code>, only print the names of files containing matches.<\/li>\n<\/ul>\n<p>For example, search &#8220;gparamspecclass&#8221; under current directory, and only show file-names and how many matches.<\/p>\n<pre>ag \"gparamspecclass\" -c<\/pre>\n<p>Or search and print the first match, as well as 2 lines before and 4 lines after the match lines.<\/p>\n<pre>ag -m 1 -A 4 -B 2 \"gparamspecclass\"<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/ag-search-match.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47947\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/ag-search-match-700x338.webp\" alt=\"\" width=\"610\" height=\"295\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/ag-search-match-700x338.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/ag-search-match-300x145.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/ag-search-match-768x370.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2025\/01\/ag-search-match.webp 786w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><code>ag<\/code> by default excludes files listed in <code>.gitignore<\/code> and <code>.hgignore<\/code> under searching directory, and user can manually add exclusions by create&#8217;n&#8217;adding them into <code>.ignore<\/code> file. <\/p>\n<p>To also search files mentioned before, either use <code>-a<\/code> option to search all (exclude hidden files) or <code>--hidden<\/code> to search hidden files. For more, see <code>man ag<\/code>.<\/p>","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to search and print which files contain your specified text or string in Ubuntu and other Linux in command line, with examples. Search file or file content is an important skill for Linux administrator. I&#8217;ve shown how to use locate command to search files through the keyword in its file-name, path, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":37909,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,9],"tags":[19],"class_list":["post-47941","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-command-skills","category-howtos","tag-command-skill"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/47941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/comments?post=47941"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/47941\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media\/37909"}],"wp:attachment":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media?parent=47941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=47941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=47941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}