How to recursively grep current directory contents, omitting all hidden directories

grep

Following the advice under this question, I am able to recursively grep for a string string within contents of a given directory dir as follows

grep -r --exclude-dir='.*' string dir

However, when I apply this to the current directory, as

$ grep -r --exclude-dir='.*' string .
$ 

I get no results, since the --exclude-dir='.*' option omits the current directory as hidden since it matches '.*'.

A work-around is to change to the parent directory via cd .., then run the command on the appropriate child, then cd again back into that original working directory. There must be a single-command that I can use to do this without the work-around of changing directories.

What is a single command to grep contents of the current directory, omitting contents of hidden directories?

Best Answer

If you’re using GNU grep 2.11 or later, -r will start from the current directory if no directory is specified:

grep -r --exclude-dir='.*' string
Related Question