Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Commands»Search a string recursively in Linux Command Line

    Search a string recursively in Linux Command Line

    RahulBy RahulJanuary 27, 20172 Mins ReadUpdated:October 6, 2020

    How do I search string recursively in the file system with grep command? Linux grep command is a frequently used command by most of the Linux users. Using grep you can search any string in all files available in the directory hierarchy. You will get come examples of grep command to search any string recursively in the file system.

    Syntax:

    Grep command uses following syntax to search pattern Recursively in all files available under specific directory and its sub directories.

    grep -R "search-pattern" /path/to/search/dir 
    

    Search Pattern Recursively in Files

    For example, you want to search a string “Hello” in all files available under /var/www directory. Also search the files under subdirectories of parent directory.

    grep -R "Hello" /var/www 
    

    This will traverse the directory tree under /var/www and print all files including line content containing string “Hello”.

    The above command will search for case sensitive string only. You can also search for strings matching in any case.

    grep -R "Hello" /var/www 
    

    This will all string in any case like “hello”, “HELLO” or HeLLo” etc.

    Search Pattern Recursively in Specific Extension Files:

    Make your search more specific, like search a string “example.com” in all files with the extension .php only. To achieve this use --include option to force grep to search for the specific file only and ignore others.

    grep -R --include="*.php" "Hello" /var/www 
    

    You may also specify multiple file extensions to search

    grep -R --include="*.php" --include="*.conf" "Hello" /var/www 
    

    Conclusion

    In this tutorial, you have learned to search a string in all files under a directory tree on Linux.

    grep grep command search
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow To Install Go 1.16 on LinuxMint 20/19/18
    Next Article How to Set Up Key-based SSH Login in Linux

    Related Posts

    How to Search Recently Modified Files in Linux

    2 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    Tee Command in Linux with Examples

    4 Mins Read

    How to Scan Open Ports with Nmap

    5 Mins Read

    Handling filenames with spaces in Linux

    3 Mins Read

    How To Compare Two Files in Linux

    Updated:August 23, 20215 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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