Linux 命令:如何“查找"仅文本文件? [英] Linux command: How to 'find' only text files?

查看:27
本文介绍了Linux 命令:如何“查找"仅文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在谷歌搜索了几次后,我得出的结论是:

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

这非常不方便,会输出不需要的文本,例如 mime 类型信息.有什么更好的解决方案吗?我在同一个文件夹中有很多图像和其他二进制文件,其中有很多我需要搜索的文本文件.

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.

推荐答案

我知道这是一个旧线程,但我偶然发现了它并认为我会分享我发现的一种非常快速的使用方法find 只查找非二进制文件:

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 . {} ; -print

grep 的 -I 选项告诉它立即忽略二进制文件,. 选项和 -q 将立即忽略匹配文本文件,所以它运行得非常快.如果您担心空格,您可以将 -print 更改为 -print0 以管道到 xargs -0 或其他内容(感谢提示,@lucas.werkmeister!)

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!)

此外,第一个点仅对于某些 BSD 版本的 find 是必需的,例如在 OS X 上,但是如果您想将它放在那里,它不会伤害任何东西别名什么的.

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.

编辑:正如@ruslan 正确指出的那样,-and 可以省略,因为它是隐含的.

EDIT: As @ruslan correctly pointed out, the -and can be omitted since it is implied.

这篇关于Linux 命令:如何“查找"仅文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆