Home > Software engineering >  Skip current directory, but allow all sub directories in grep
Skip current directory, but allow all sub directories in grep

Time:07-22

How do I skip grep searching files in the current directory, but search files in all the sub directories?

CodePudding user response:

Use a wildcard that matches all the subdirectories as the argument.

grep -R pattern */

The / at the end forces it to only match directories (including symbolic links to directories).

CodePudding user response:

I guess the following could do:

grep -R pattern $(find -type d -maxdepth 1)

Here we use $(find -type d -maxdepth 1) to get the list of subdirectories in the current directory.

Update: one difference to the nice Barmar's solution is that find would also fetch hidden subdirectories with names starting with a dot.

  • Related