开发者

Unix find with wildcard directory structure

开发者 https://www.devze.com 2023-03-24 23:01 出处:网络
I am trying to do a find where I can specify wildcards in the directory structure then do a grep for www.do开发者_运维知识库main.com in all the files within the data directory.

I am trying to do a find where I can specify wildcards in the directory structure then do a grep for www.do开发者_运维知识库main.com in all the files within the data directory.

ie

find /a/b/c/*/WA/*/temp/*/*/data -type f -exec grep -l "www.domain.com" {} /dev/null \;

This works fine where there is only one possible level between c/*/WA.

How would I go about doing the same thing above where there could be multiple levels between C/*/WA?

So it could be at

/a/b/c/*/*/WA/*/temp/*/*/data 

or

/a/b/c/*/*/*/WA/*/temp/*/*/data

There is no defined number of directories between /c/ and /WA/; there could be multiple levels and at each level there could be the /WA/*/temp/*/*/data.

Any ideas on how to do a find such as that?


How about using a for loop to find the WA directories, then go from there:

for DIR in $(find /a/b/c -type d -name WA -print); do
    find $DIR/*/temp/*/*/data -type f \
        -exec grep -l "www.domain.com" {} /dev/null \;
done

You may be able to get all that in a single command, but I think clarity is more important in the long run.


Assuming no spaces in the paths, then I'd think in terms of:

find /a/b/c -name data -type f |
grep -E '/WA/[^/]+/temp/[^/]+/[^/]+/data' |
xargs grep -l "www.domain.com" /dev/null

This uses find to find the files (rather than making the shell do most of the work), then uses the grep -E (equivalent to egrep) to select the names with the correct pattern in the path, and then uses xargs and grep (again) to find the target pattern.

0

精彩评论

暂无评论...
验证码 换一张
取 消