Finding human-readable files on unix

旧城冷巷雨未停 提交于 2019-12-03 00:32:42

find and file are your friends here:

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.

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.

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

How about

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

find will give you a list of files.

xargs file will run the file command on each of the lines from the piped input.

i use

    file directory/to/search/* 

For example to find only human readable files in a directory called home use:

    file home/* 

and the readable file will have a format such as ASCII text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!