Linux命令:如何只查找文本文件? [英] Linux command: How to 'find' only text files?
问题描述
find my_folder -type f -exec grep - lneedle text{} \; -exec文件{} \; | grep文本
这是非常不方便的,并输出不需要的文本,如MIME类型信息。有更好的解决方案我有很多的图像和其他二进制文件在同一个文件夹中有很多文本文件,我需要通过搜索。
我知道这是一个古老的线程,但我偶然发现它,并认为我会分享我的方法,我发现这是一个非常快速的方式来使用 find
来查找只有非二进制文件:
find。 -type f -exec grep -Iq。 {} \; -and -print
-I
选项grep告诉它立即忽略二进制文件,。
选项和 -q
会立即匹配文本文件,所以它变得非常快。您可以将 -print
更改为一个 -print0
,以便管道到 xargs -0
或者如果你关心的是空格(感谢提示,@ lucas.werkmeister!)
也是第一个点BSD版本的找到
,比如在OS X上,但是如果你想把它放在一个别名或者别的地方,它不会伤害任何东西。 / p>
After a few searches from Google, what I come up with is:
find my_folder -type f -exec grep -l "needle text" {} \; -exec file {} \; | grep text
which is very unhandy and outputs unneeded texts such as mime type information. Any better solutions? I have lots of images and other binary files in the same folder with a lot of text files that I need to search through.
I know this is an old thread, but I stumbled across it and thought I'd share my method which I have found to be a very fast way to use find
to find only non-binary files:
find . -type f -exec grep -Iq . {} \; -and -print
The -I
option to grep tells it to immediately ignore binary files and the .
option along with the -q
will make it immediately match text files so it goes very fast. You can change the -print
to a -print0
for piping into an xargs -0
or something if you are concerned about spaces (thanks for the tip, @lucas.werkmeister!)
Also the first dot is only necessary for certain BSD versions of find
such as on OS X, but it doesn't hurt anything just having it there all the time if you want to put this in an alias or something.
这篇关于Linux命令:如何只查找文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!