我想在Linux机器上寻找没有文件扩展名限制的易于人类阅读的文件。这些文件应该是人类可感知的,例如文本、配置、HTML、源代码等文件。是否有一种过滤和定位的方法?
我想在Linux机器上寻找没有文件扩展名限制的易于人类阅读的文件。这些文件应该是人类可感知的,例如文本、配置、HTML、源代码等文件。是否有一种过滤和定位的方法?
用途:
find /dir/to/search -type f | xargs file | grep text
find
将提供文件列表。
xargs file
将在管道输入的每行上运行 file
命令。
find /dir/to/search -type f -print0 | xargs -0 file | grep text
... 什么是有趣?嵌入空格、括号、方括号、花括号等。 - tinkfind /dir/to/search -type f -exec sh -c 'file -b {} | grep text &>/dev/null' \; -print
这将在/dir/to/search中查找任何文件(注意:它不会查找符号链接目录套接字等,仅查找普通文件),并运行sh -c'file -b {} | grep text & >/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 file。
grep
的末尾使用"&"?我的理解是这会使grep异步运行。这样做是否仍会将退出状态传递给find
?为什么要这样做?感谢您抽出时间来回答。 - Jesse Emond&
标记,该标记会将作业放到后台,它包含一个&>
标记,该标记会导致stdout和stderr的重定向。 - Ben Voigt这个也应该可以正常工作:
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
file
工具在确定文件内容类型方面非常出色。也许您可以使用它并根据其输出过滤文件。 - cdhowie