Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»How to find all files containing specific text in Linux

    How to find all files containing specific text in Linux

    By RahulAugust 18, 20223 Mins Read

    This tutorial will teach you how to recursively search for files containing a specific string on Linux using the command line. This tutorial uses the ‘grep’ command to search for strings in files. Alternatively, you may use the find command to look for files with specific content.

    Advertisement

    A basic syntax for searching text with grep command:

    1
    grep -rl "search-string" /path/to/serch/dir

    The grep command offers other useful options for finding specific text in file systems.

    • -r, --recursive: Search files recursively
    • -R, --dereference-recursive: Search files recursively and follow symlinks
    • --include=FILE_PATTERN: search only files that match FILE_PATTERN
    • --exclude=FILE_PATTERN: skip files and directories matching FILE_PATTERN
    • --exclude-from=FILE: skip files matching any file pattern from FILE
    • --exclude-dir=PATTERN: directories that match PATTERN will be skipped.
    • -L, --files-without-match: Print file names containing no match
    • -l, --files-with-matches: Print string containing file names only
    • -i, --ignore-case: ignore case of search string
    • -e, --regexp=PATTERN: Use a pattern to search or specify multiple search strings
    • -w, --word-regexp: force to match whole words

    There are several ways to use the grep command to search text. Let’s discuss a few examples of searching a text/string in the file system.

    1. Search Single String in All Files
    2. The below example command will search the string “Error” in all files in /var/log directory and its sub-directories.

      grep -rlw "Error" /var/log 
      
      Search specific text in all files in Linux
      Search specific text in all files using command line

    3. Search Multiple String in All Files
    4. The -e switch can also be utilized to find multiple strings. This is comparable to the egrep program. The example below will look for “Error” and “Warning” in all the files in the /var/log directory and its subdirectories.

      grep -rlw -e "Error" -e "Warning"  /var/log 
      
      Search multiple string in all files in Linux
      Search multiple string in all files with command line

    5. Search String in Specific Files
    6. You can search strings in files that match the file name criteria. The following command searches for “Error” in files with the .log extension in the /var/log directory and its sub-directories.

      grep -rlw --include="*.log" -e "Error" /var/log 
      

    7. Exclude Some Files from Search
    8. You can use the --exclude option in find to exclude some files that match certain file name criteria. For example, you can exclude files with the .txt extension.

      grep -rlw --exclude="*.txt" -e "tecadmin" /var/log 
      

    9. Exclude Some Directories from Search
    10. You can also skip searching certain directories. For instance, don’t search for string files in any folder with apache2 in its name.

      grep -rlw --exclude-dir="*apache2*" -e "tecadmin" /var/log 
      

    Conclusion

    You have learned how to search for specific text in files on the Linux file system in this tutorial.

    command grep linux search string
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    free Command in Linux (Check Memory Uses)

    A Practical Guide to Extracting Compressed Files in Linux

    TR Command in Linux: A Guide with Practical Examples

    TR Command in Linux: A Guide with Practical Examples

    View 6 Comments

    6 Comments

    1. Jim Harris on December 18, 2021 9:05 pm

      Another useful command switch is “-s”
      This suppresses errors where files are not readable, or there are permission errors, etc.

      Reply
    2. Simon on July 10, 2019 9:23 am

      very helpful & clear: Many thanks!

      Reply
    3. Hal on February 2, 2018 3:54 pm

      Good job! really helpful.

      Regards
      Hal

      Reply
    4. Jayant on February 1, 2018 9:45 am

      Really helpful. Good Job

      Thanks

      Reply
    5. DStevenson on June 14, 2017 6:11 pm

      Well laid out. Thank you.

      Reply
    6. Samradh on May 12, 2017 4:24 pm

      Thanks, very helpful…

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Setting Up Angular on Ubuntu: Step-by-Step Guide
    • Converting UTC Date and Time to Local Time in Linux
    • Git Restore: Functionality and Practical Examples
    • Git Switch: Functionality and Practical Examples
    • Git Switch vs. Checkout: A Detailed Comparison with Examples
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.