The grep
command is a great tool to search all directories and subdirectories for their contents. The grep command recursive option is used to make a search in all specified paths and subdirectories for all files and child files for the specified term.
Search Current Working Directory Recursively with grep Command
The first example of searching recursively with grep is searching the current working directory. The current working directory is expressed with the dot. In the following example, we will search recursively the current working directory. The -r
option is used for recursive search. We will search for the term linuxtect
.
$ grep -r "linuxtect" .
Search Specified Relative Path Recursively with grep Command
The grep command can be used to search recursively for the specified relative path. Relative paths are used to specify files and folders according to the current working directory. In the following example, we search the current working directory’s parent directory.
$ grep -r "linuxtect" ..
Search Specified Absolute Path Recursively with grep Command
Another way to search recursively with the grep command is searching with the absolute path. The absolute path specifies the complete path without any relativity. Using absolute path also prevents mistakes and can be used from any working directory. In the following example, we will search with the absolute path /home/ismail
.
$ grep -r "linuxtect" /home/ismail
Search Recursively and Case-insensitive
The grep command provides different attributes where we can also search as case-insensitive mode. The -i
option is used for the case-insensitive search.
$ grep -i -r "linuxtect" /home/ismail