I often need to search for a particular string from a directory that has git directory .git/
. This is the command I use:
find . -name .git -prune -o -print | xargs grep <string-to-search>
The -name .git -prune -o -print
option is to ignore the files under .git/
. However, it outputs a lot of 'Is a directory' messages that clutter the whole result. For example,
...
grep: ./reports: Is a directory
grep: ./scripts: Is a directory
grep: ./scripts/js: Is a directory
grep: ./scripts/js/adapter: Is a directory
...
How to modify the command line so that I can ignore the .git/
directory and all other directories (i.e. only search the searchable files).
Apparently, the -type f
option doesn't work well in:
find -type f . -name .git -prune -o -print
To Search 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.
Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. Using the grep command, we can recursively search all files for a string on a Linux.
Recursive Search 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.
You don't require find command. You can use grep to search in directory. You can ignore directory by using exclude-dir syntax.
grep -r --exclude-dir=".git;.svn" "string to search" <directory>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With