Often, we want to find if a directory structure contains a file with a given string pattern. This is specially useful for developers looking at a big code trying to connect various pieces of codes.
Here,
Suppose, now one wants to find out if a certain string is present in certain file types only. Suppose we want to search for word php in .php and .xml files only. Then, we use a command like
grep -rnw "STRING" \path\
Here,
- r is for recursive, n is for line number and w is for matching whole word.
- STRING is the pattern one is looking for.
- replace \path\ by the directory you are searching in. Use '.' without quotes if it is the current directory.
Suppose, now one wants to find out if a certain string is present in certain file types only. Suppose we want to search for word php in .php and .xml files only. Then, we use a command like
grep -rnw --include=*.{php,xml} "php" .--exclude is used to exclude certain file types or directories from the search in the similar fashion.