HowTo: grep All Sub-directory For Files

How do I grep all sub directories? How can I use grep command through all sub-directories on Linux or Unix like operating systems? How do I use grep to find pattern including all subdirectories on OS X/BSD/Unix-like operating systems?

You can use grep command to search files for given words on a Linux or Unix-like system. The basic syntax is as follows:

ADVERTISEMENTS

Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements grep+Terminal
Time 1m
grep 'word' file
grep 'word1 word2' file1 file2
grep [option] 'word1 word2' file1 file2

You can search all text files in the current directory with wild cards:

grep 'word-to-search' *

Search sub directories recursively using grep

Pass the -r option to grep command to search recursively through an entire directory tree. With this option one can search the current directory and and all levels of subdirectories by passing the -r or -R to the grep command. The syntax is:

grep -r 'word-to-search' *

OR

grep -R 'word-to-search' *

Examples

For example, the following would search all files in the current directory and in all of its subdirectories for every line containing the word ‘main()’:

grep -r 'main()' *

The following example would search for /etc/ and all of its subdirectories for every line containing the ip address ‘192.168.1.254’:

grep -r '192.168.1.254' /etc/

Other options

You can use the find command as follows:

find /dir/to/search -type f -iname "pattern" -print0 | xargs -I {} -0 grep --color 'word-to-search' "{}"

For example, search for an IP address ‘192.168.1.254’ in all *.conf files located in /usr:

## must run as root #
find /usr/ -type f -iname '*.conf' -print0 | xargs -I {} -0 grep -H --color '192.168.1.5' "{}"
Recommend readings
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
2 comments… add one
  • kustodian Dec 20, 2015 @ 10:10

    First example is incorrect and everything with * as a current directory. “grep -r ‘main()’ *” wouldn’t search all files in the current directory and in all of its subdirectories for every line, containing the word ‘main()’, but would search in all visible files and visible directories and their subdirectories in the current directory, it would ignore files/dirs starting with a dot (invisible files). To do what you want in the example, you should use dot ‘.’ instead of the asterisk:

    grep -r ‘main()’ .

  • Pranay Bankar Jan 6, 2017 @ 9:29

    Hi,

    How do I search a keyword in all jar files inside an directory?
    Please do let me know. Thanks in advance.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.