Linux / UNIX Recursively Search All Files For A String

Linux / UNIX Recursively Search All Files For A String

To recursively search all files in a directory and its subdirectories for a specific string in Linux or Unix, you can use the grep command with the -r option.

For example, to search for the string "hello" in all files in the current directory and its subdirectories, you can use the following command:

grep -r "hello" .
S‮‬ource:www.lautturi.com

This will search for the string "hello" in all files in the current directory and its subdirectories, and print the names of the files and the lines containing the string to the terminal.

You can use the -l option to print only the names of the files that contain the string, rather than the matching lines themselves:

grep -rl "hello" .

You can also use the -i option to perform a case-insensitive search, and the -w option to match only whole words.

For example:

grep -rliw "hello" .

This will search for the string "hello" in a case-insensitive manner, and match only whole words, in all files in the current directory and its subdirectories, and print the names of the files that contain the string.

You can use the -n option to print the line numbers of the matching lines, and the -H option to print the file names of the matching files even when there is only one file being searched.

You can also use the -E option to use extended regular expressions in your search pattern. For example:

grep -rEHn "hello|world" .

This will search for either the string "hello" or the string "world" in all files in the current directory and its subdirectories, and print the file names, line numbers, and the matching lines.

For more information about the grep command and its options, you can refer to the grep man page.

Created Time:2017-10-29 22:08:55  Author:lautturi