Solve grep is directory problem under Linux

Grep is a very convenient and useful terminal tool, which can help us quickly filter out some content. Usually with the find command, more powerful capabilities can be achieved.

For example, this combination can quickly find and locate which .gradle file contains maven.aliyun.com.

 1 
find . -name "*.gradle" | xargs grep -E -n --color = always "maven.aliyun.com"

But when we execute it, we always encounter such an error prompt output.

 1 
grep: ./example/android/.gradle: Is a directory

This problem occurs because find . -name "*.gradle" matches the .gradle directory, and it doesn’t make sense to use grep to simply scan this directory (without containing internal files).

There are still many solutions that can easily circumvent this error output.

Method 1, recursively find the directory

We can use the -r command to recursively search for files inside the directory

 1 2 3 4 5 
find . -name "*.gradle" | xargs grep -E -n --color = always -r "maven.aliyun.com" ./example/android/.gradle/build.gradle:7: url 'http://maven.aliyun.com/nexus/content/repositories/releases/' ./example/android/.gradle/build.gradle:23: url 'http://maven.aliyun.com/nexus/content/repositories/releases/' ./example/android/.gradle/build.gradle:7: url 'http://maven.aliyun.com/nexus/content/repositories/releases/' ./example/android/.gradle/build.gradle:23: url 'http://maven.aliyun.com/nexus/content/repositories/releases/'

Method 2, skip the directory

We can skip looking for folders with --directories=skip

 1 2 3 4 
find . -name "*.gradle" | xargs grep -E -n --color = always "maven.aliyun.com" --directories = skip find . -name "*.gradle" | xargs grep -E -n --color = always "maven.aliyun.com" --directories = skip ./example/android/.gradle/build.gradle:7: url 'http://maven.aliyun.com/nexus/content/repositories/releases/' ./example/android/.gradle/build.gradle:23: url 'http://maven.aliyun.com/nexus/content/repositories/releases/'

Option three, set environment variables, skip directories

 1 2 3 4 
export GREP_OPTIONS = "--directories=skip" find . -name "*.gradle" | xargs grep -E -n --color = always "maven.aliyun.com" ./example/android/.gradle/build.gradle:7: url 'http://maven.aliyun.com/nexus/content/repositories/releases/' ./example/android/.gradle/build.gradle:23: url 'http://maven.aliyun.com/nexus/content/repositories/releases/'

Option 4, grep input source control

We can use find’s -type f to find only file types, excluding directory types.

 1 2 3 
find . -name "*.gradle" -type f | xargs grep -E -n --color = always "maven.aliyun.com" ./example/android/.gradle/build.gradle:7: url 'http://maven.aliyun.com/nexus/content/repositories/releases/' ./example/android/.gradle/build.gradle:23: url 'http://maven.aliyun.com/nexus/content/repositories/releases/'

All of the above methods can solve this problem, and you can choose the preferred solution for processing and use at will.

droidyue_gzh_green_png.png

This article is reprinted from https://droidyue.com/blog/2022/06/26/fix-grep-is-directory-issue-linux-mac/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment