Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep a string in a directory and all its subdirectories? [duplicate]

People also ask

How do you grep a string from all subdirectories?

To include all subdirectories in a search, add the -r operator to the grep command. This command prints the matches for all files in the current directory, subdirectories, and the exact path with the filename.

How do you search for a string in a directory with the subdirectories recursively?

To recursively search for a pattern, invoke grep with the -r option (or --recursive ). When this option is used grep will search through all files in the specified directory, skipping the symlinks that are encountered recursively.

Which command will you choose to copy all files and subdirectories?

To copy a directory with all subdirectories and files, use the cp command.

Which command is used to search all directory and subdirectories?

Options. The find command will begin looking in the starting directory you specify and proceed to search through all accessible subdirectories. You may specify more than one starting directory for searching.


If your grep supports -R, do:

grep -R 'string' dir/

If not, then use find:

find dir/ -type f -exec grep -H 'string' {} +

grep -r -e string directory

-r is for recursive; -e is optional but its argument specifies the regex to search for. Interestingly, POSIX grep is not required to support -r (or -R), but I'm practically certain that System V grep did, so in practice they (almost) all do. Some versions of grep support -R as well as (or conceivably instead of) -r; AFAICT, it means the same thing.