{"id":3382,"date":"2024-05-22T19:02:23","date_gmt":"2024-05-22T13:32:23","guid":{"rendered":"https:\/\/www.veeble.org\/kb\/?p=3382"},"modified":"2025-04-09T12:46:22","modified_gmt":"2025-04-09T07:16:22","slug":"how-to-find-files-containing-specific-text-in-linux","status":"publish","type":"post","link":"https:\/\/www.veeble.com\/kb\/how-to-find-files-containing-specific-text-in-linux\/","title":{"rendered":"How to Find Files Containing Specific Text in Linux"},"content":{"rendered":"\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/05\/Linux-Symbol.jpg\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1196\" height=\"470\" data-id=\"3411\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/05\/Linux-Symbol.jpg\" alt=\"Searching for Text Within Files in Linux\" class=\"wp-image-3411\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/Linux-Symbol.jpg 1196w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/Linux-Symbol-300x118.jpg 300w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/Linux-Symbol-1024x402.jpg 1024w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/Linux-Symbol-768x302.jpg 768w\" sizes=\"auto, (max-width: 1196px) 100vw, 1196px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<p>Sometimes, you need to find a file that contains a specific word or phrase. Linux provides powerful tools to help you search for text within files. This guide will show you how to use these tools.<\/p>\n\n\n\t\t\t\t<div class=\"wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-80c0e9e5      \"\n\t\t\t\t\tdata-scroll= \"1\"\n\t\t\t\t\tdata-offset= \"30\"\n\t\t\t\t\tstyle=\"\"\n\t\t\t\t>\n\t\t\t\t<div class=\"uagb-toc__wrap\">\n\t\t\t\t\t\t<div class=\"uagb-toc__title\">\n\t\t\t\t\t\t\tTable Of Contents\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"uagb-toc__list-wrap \">\n\t\t\t\t\t\t<ol class=\"uagb-toc__list\"><li class=\"uagb-toc__list\"><a href=\"#grep-command\" class=\"uagb-toc-link__trigger\">Grep command<\/a><li class=\"uagb-toc__list\"><a href=\"#find-command\" class=\"uagb-toc-link__trigger\">Find Command<\/a><li class=\"uagb-toc__list\"><a href=\"#ack-command\" class=\"uagb-toc-link__trigger\">ACK Command<\/a><li class=\"uagb-toc__list\"><a href=\"#ag-command\" class=\"uagb-toc-link__trigger\">AG Command<\/a><\/ol>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"grep-command\">Grep command<\/h4>\n\n<h6 class=\"wp-block-heading\" id=\"how-to-find-files-containing-specific-text-with-grep-command\">How to Find Files Containing Specific Text with Grep Command?<\/h6>\n\n\n<p><code><a href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/grep.1.html\" target=\"_blank\" rel=\"noopener\">grep<\/a><\/code> is a command-line utility for searching plain-text data for lines that match a regular expression. It\u2019s very powerful and commonly used.<br>To search for a specific text containing files within the current directory, open your terminal and type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -l 'your_text' *<\/code><\/pre>\n\n\n\n<p>Replace <code>your_text<\/code> with the text you\u2019re searching for. This command will list all lines contained <code>your_text<\/code> in the files within the current directory.<\/p>\n\n\n\n<p>Example: Searching the keyword &#8220;five centuries&#8221; in the current directory files:<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-183948.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"521\" height=\"69\" data-id=\"3418\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-183948.png\" alt=\"find which file contains the specific text in linux\" class=\"wp-image-3418\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-183948.png 521w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-183948-300x40.png 300w\" sizes=\"auto, (max-width: 521px) 100vw, 521px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<p><strong>Searching Recursively<\/strong>:<br>If you want to search through all files in the current directory and all its subdirectories, use the <code>-r<\/code> (recursive) option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -lr 'your_text' .<\/code><\/pre>\n\n\n\n<p>The dot <code>.<\/code> tells <code>grep<\/code> to start searching from the current directory and go through all subdirectories.<\/p>\n\n\n\n<p>Example: Searching the keyword &#8220;five centuries&#8221; in the directories recursively:<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-3 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-184303.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"466\" height=\"67\" data-id=\"3419\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-184303.png\" alt=\"find which file contains the specific text in linux\" class=\"wp-image-3419\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-184303.png 466w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-184303-300x43.png 300w\" sizes=\"auto, (max-width: 466px) 100vw, 466px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<p><strong>Ignoring Case<\/strong>:<br>To ignore case (uppercase or lowercase) while searching, add the <code>-i<\/code> option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -rli 'your_text' .<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"find-command\">Find Command<\/h4>\n\n<h6 class=\"wp-block-heading\" id=\"how-to-find-files-containing-specific-text-with-find-command\">How to Find Files Containing Specific Text with Find Command?<\/h6>\n\n\n<p>Sometimes, you might want to search only specific types of files. You can combine <code><a href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/find.1.html\" target=\"_blank\" rel=\"noopener\">find<\/a><\/code> with <code>grep<\/code> for more targeted searches.<br>For example, to find all <code>.txt<\/code> files containing <code>your_text<\/code>, use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -name \"*.txt\" -exec grep -il 'your_text' {} \\;<\/code><\/pre>\n\n\n\n<p>This command will search for <code>.txt<\/code> files in the current directory and all subdirectories, then <code>grep<\/code> will search within those files for <code>your_text<\/code>.<\/p>\n\n\n\n<p>Example: Finding the PHP files containing the text &#8220;five centuries&#8221; in the current directory:<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-4 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-185138.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"756\" height=\"62\" data-id=\"3420\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-185138.png\" alt=\"Finding the PHP files containing the specific text in linux current directory:\" class=\"wp-image-3420\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-185138.png 756w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-185138-300x25.png 300w\" sizes=\"auto, (max-width: 756px) 100vw, 756px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"ack-command\">ACK Command<\/h4>\n\n<h6 class=\"wp-block-heading\" id=\"how-to-find-files-containing-specific-text-with-ack-command\">How to Find Files Containing Specific Text with ACK Command?<\/h6>\n\n\n<p><code><a href=\"https:\/\/linux.die.net\/man\/1\/ack#:~:text=Description,ack%20prints%20the%20matching%20lines.\" target=\"_blank\" rel=\"noopener\">ack<\/a><\/code> is a tool similar to <code>grep<\/code> but optimized for searching source code. <br>You might need to install <code>ack<\/code> tool first. On Debian-based systems (like Ubuntu), you can install it with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install ack<\/code><\/pre>\n\n\n\n<p>After installation, search for <code>your_text<\/code> using <code>ack<\/code>, just type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ack -il 'your_text'<\/code><\/pre>\n\n\n\n<p>This will search for <code>your_text<\/code> in all files in the current directory and subdirectories.<\/p>\n\n\n\n<p>Example: Searching the keyword &#8220;five centuries&#8221; in the directories recursively using ack command:<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-5 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-185803.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"511\" height=\"68\" data-id=\"3422\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-185803.png\" alt=\"finding linux file with a specific keyword using ack command\" class=\"wp-image-3422\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-185803.png 511w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-185803-300x40.png 300w\" sizes=\"auto, (max-width: 511px) 100vw, 511px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"ag-command\">AG Command<\/h4>\n\n<h6 class=\"wp-block-heading\" id=\"how-to-find-files-containing-specific-text-with-ag-command\">How to Find Files Containing Specific Text with AG Command?<\/h6>\n\n\n<p><code><a href=\"https:\/\/manpages.ubuntu.com\/manpages\/focal\/en\/man1\/ag.1.html\" target=\"_blank\" rel=\"noopener\">ag<\/a><\/code> is another tool similar to <code>ack<\/code>, and it is known for its speed.<br>You might need to install <code>ag<\/code> first. On Debian-based systems, you can install it with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install silversearcher-ag<\/code><\/pre>\n\n\n\n<p>After installation, search for <code>your_text<\/code> using <code>ag<\/code>, just type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ag -il 'your_text'<\/code><\/pre>\n\n\n\n<p>This will search for <code>your_text<\/code> in all files in the current directory and subdirectories.<\/p>\n\n\n\n<p>Example: Searching the keyword &#8220;five centuries&#8221; in the directories recursively using ag command:<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-6 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-190043.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"512\" height=\"61\" data-id=\"3423\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-190043.png\" alt=\"Searching a specific keyword in the directories recursively using ag command\" class=\"wp-image-3423\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-190043.png 512w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-22-190043-300x36.png 300w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n\n\n\n<p>Using these tools, you can quickly find files containing specific text in Linux. Whether you use <code>grep<\/code>, <code>find<\/code>, <code>ack<\/code>, or <code>ag<\/code>, each has its strengths and is suited for different scenarios.<\/p>\n\n\n\n<div style=\"height:35px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-61220be1 wp-block-button\"><div class=\"uagb-cta__wrap\"><h3 class=\"uagb-cta__title\">Experience the Power of Managed VPS Hosting with Veeble<\/h3><p class=\"uagb-cta__desc\">Experience the difference of a truly managed VPS hosting solution. With Veeble, you&#8217;ll get 24\/7 priority support, expert security, and a suite of powerful tools to help you manage your website.<\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/www.veeble.com\/in\/managed-vps\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_blank\" rel=\"noopener noreferrer\">Get Started Today<svg xmlns=\"https:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\"><path d=\"M504.3 273.6l-112.1 104c-6.992 6.484-17.18 8.218-25.94 4.406c-8.758-3.812-14.42-12.45-14.42-21.1L351.9 288H32C14.33 288 .0002 273.7 .0002 255.1S14.33 224 32 224h319.9l0-72c0-9.547 5.66-18.19 14.42-22c8.754-3.809 18.95-2.075 25.94 4.41l112.1 104C514.6 247.9 514.6 264.1 504.3 273.6z\"><\/path><\/svg><\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, you need to find a file that contains a specific word or phrase. Linux provides powerful tools to help you search for text [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":8532,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[5],"tags":[],"class_list":["post-3382","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"uagb_featured_image_src":{"full":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/How-to-Find-Files-Containing-Specific-Text-in-Linux.jpg",1536,1024,false],"thumbnail":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/How-to-Find-Files-Containing-Specific-Text-in-Linux-150x150.jpg",150,150,true],"medium":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/How-to-Find-Files-Containing-Specific-Text-in-Linux-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/How-to-Find-Files-Containing-Specific-Text-in-Linux-768x512.jpg",768,512,true],"large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/How-to-Find-Files-Containing-Specific-Text-in-Linux-1024x683.jpg",1024,683,true],"1536x1536":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/How-to-Find-Files-Containing-Specific-Text-in-Linux.jpg",1536,1024,false],"2048x2048":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/05\/How-to-Find-Files-Containing-Specific-Text-in-Linux.jpg",1536,1024,false]},"uagb_author_info":{"display_name":"Adrian Antony","author_link":"https:\/\/www.veeble.com\/kb\/author\/adrian\/"},"uagb_comment_info":0,"uagb_excerpt":"Sometimes, you need to find a file that contains a specific word or phrase. Linux provides powerful tools to help you search for text [&hellip;]","_links":{"self":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/3382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/comments?post=3382"}],"version-history":[{"count":19,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/3382\/revisions"}],"predecessor-version":[{"id":7494,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/3382\/revisions\/7494"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media\/8532"}],"wp:attachment":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media?parent=3382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/categories?post=3382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/tags?post=3382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}