Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Search: Using find / grep, While Ignoring .git and Other Directories

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
like image 798
moey Avatar asked Jan 01 '13 09:01

moey


People also ask

How do I use grep to search all files in a directory?

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.

How do you search for a string in all files recursively in Linux?

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.

How do I grep all files in a directory recursively?

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.


1 Answers

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>
like image 90
Vivek Goel Avatar answered Oct 02 '22 09:10

Vivek Goel