在 Unix 上查找人类可读的文件

问题描述 投票:0回答:3

我想在我的 Linux 机器上找到人类可读的文件,而没有文件扩展名限制。这些文件应该可供人们使用文本编辑器读取,例如:文本、配置、HTML 和源代码文件。

有没有办法过滤和定位它们?

linux find human-readable
3个回答
25
投票

用途:

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

find
将为您提供文件列表。

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


8
投票

查找文件是您的朋友:

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

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

注意:使用 -b 标志来文件意味着不会打印文件名,因此不会对 grep 产生任何问题。例如,如果没有

-b
标志,二进制文件 gettext 将被错误地检测为文本文件。

例如,

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

如果您想查看压缩文件,请使用

--uncompress
标志进行归档。有关更多信息和要归档的标志,请参阅 man 文件


0
投票

这也应该可以正常工作:

file_info=`file "$file_name"` # First reading the file info string which should have the words "ASCII" or "Unicode" if it's a readable file

if grep -q -i -e "ASCII" -e "Unicode"<<< "$file_info"; then
    echo "file is readable"
fi
© www.soinside.com 2019 - 2024. All rights reserved.