我想列出并排序目录中所有 ASCII 文本文件(但不一定具有 .txt 扩展名)。我该怎么做?我是 Shell 脚本新手。
答案1
使用file
命令:
#!/bin/bash
for i in * # for every file in folder
do
# if file reports "ASCII text"
if file "$i" | grep --quiet 'ASCII text$'
then
# print filename
echo "$i"
fi
done
一句话:
$ for i in *; do if file "$i" | grep -q 'ASCII text$' ; then echo "$i"; fi; done