Linux 在 unix 上查找人类可读的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14505218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:47:55  来源:igfitidea点击:

Finding human-readable files on unix

linuxfindhuman-readable

提问by Yi?it

I'd like to find human-readable files on my linux machine without a file extension constraint. Those files should be of human sensing files like text, configuration, html, source-code etc. files. Could you suggest a way to filter and locate.

我想在没有文件扩展名限制的情况下在我的 linux 机器上找到人类可读的文件。这些文件应该是人类感知文件,如文本、配置、html、源代码等文件。你能提出一种过滤和定位的方法吗?

采纳答案by peteches

find and file are your friends here:

find 和 file 是你的朋友在这里:

find /dir/to/search -type f -exec sh -c 'file -b {} | grep text &>/dev/null' \; -print

this will find any files ( NOTE: it will not find symlinks directories sockets etc only regular files ) in /dir/to/search and run sh -c 'file -b {} | grep text &>/dev/null' \; which looks at the type of file and looks for text in the description. if this returns true ( ie text is in the line) then it prints the filename.

这将在 /dir/to/search 中找到任何文件(注意:它不会找到符号链接目录套接字等仅常规文件)并运行 sh -c 'file -b {} | grep text &>/dev/null' \; 它查看文件类型并在描述中查找文本。如果返回 true(即文本在行中),则它会打印文件名。

NOTE: using the -b flag to filemeans that the filename is not printed and therefore cannot create any issues with the grep. eg without the -b flag the binary file gettext would erroneously be detected as a textfile.

注意:对文件使用 -b 标志意味着不会打印文件名,因此不会对 grep 产生任何问题。例如,如果没有 -b 标志,二进制文件 gettext 将被错误地检测为文本文件。

eg

例如

root@osdevel-pete# find /bin -exec sh -c 'file -b {} |  grep text &>/dev/null' \; -print
/bin/gunzip
/bin/svnshell.sh
/bin/unicode_stop
/bin/unicode_start
/bin/zcat
/bin/redhat_lsb_init
root@osdevel-pete# find /bin -type f -name *text*
/bin/gettext

EDIT:

编辑:

If you want to look in compressed files use the --uncompress flag to file. for more info and flags to file see man file

如果要查看压缩文件,请使用 --uncompress 标志来归档。有关要归档的更多信息和标志,请参阅man 文件

回答by Blam

How about

怎么样

find /dir/to/search -type f | xargs file | grep text

find /dir/to/search -type f | xargs file | grep text

findwill give you a list of files.

find会给你一个文件列表。

xargs filewill run the filecommand on each of the lines from the piped input.

xargs filefile在管道输入的每一行上运行命令。

回答by because_im_batman

Just wanted to share that this should work fine too.

只是想分享一下,这也应该可以正常工作。

file_info=`file "$file_name"` # first reading the file info string which should have the words "ASCII" or "Unicode" if its a readable file 

if grep -q -i -e "ASCII" -e "Unicode"<<< "$file_info"; then
        echo "file is readable"
fi