Find all files matching 'name' on linux sy

2019-02-01 23:07发布

I need to find all instances of 'filename.ext' on a linux system and see which ones contain the text 'lookingfor'.

Is there a set of linux command line operations that would work?

5条回答
Rolldiameter
2楼-- · 2019-02-01 23:30

A more simple one would be,

find / -type f -name filename.ext -print0 | xargs -0 grep  'lookingfor'

-print0 to find & 0 to xargs would mitigate the issue of large number of files in a single directory.

查看更多
闹够了就滚
3楼-- · 2019-02-01 23:30

Try:

find / -type f -name filename.ext -exec grep -H -n 'lookingfor' {} \;

find searches recursively starting from the root / for files named filename.ext and for every found occurrence it runs grep on the file name searching for lookingfor and if found prints the line number (-n) and the file name (-H).

查看更多
Anthone
4楼-- · 2019-02-01 23:37

Go to respective directory and type the following command.

find . -name "*.ext" | xargs grep 'lookingfor'

查看更多
▲ chillily
5楼-- · 2019-02-01 23:43
find / -type f -name filename.ext -exec grep -l 'lookingfor' {} +

Using a + to terminate the command is more efficient than \; because find sends a whole batch of files to grep instead of sending them one by one. This avoids a fork/exec for each single file which is found.

A while ago I did some testing to compare the performance of xargs vs {} + vs {} \; and I found that {} + was faster. Here are some of my results:

time find . -name "*20090430*" -exec touch {} +
real    0m31.98s
user    0m0.06s
sys     0m0.49s

time find . -name "*20090430*" | xargs touch
real    1m8.81s
user    0m0.13s
sys     0m1.07s

time find . -name "*20090430*" -exec touch {} \;
real    1m42.53s
user    0m0.17s
sys     0m2.42s
查看更多
别忘想泡老子
6楼-- · 2019-02-01 23:44

I find the following command the simplest way:

grep -R --include="filename.ext" lookingfor

or add -i to search case insensitive:

grep -i -R --include="filename.ext" lookingfor
查看更多
登录 后发表回答