我有一个文件夹,其中包含文件列表。问题是他们没有扩展。我想检查它们是 JPEG 格式还是 JPG 格式。我试过这个
find folder -type f -not -name "*.*"
输出为
folder/t351
folder/t352
folder/t353
folder/t354
folder/t355
我尝试使用正则表达式,但由于文件名没有扩展名,因此无法解决。有人可以告诉我如何验证文件并检查它们是否具有某种格式。
您应该使用 file 命令。我向你推荐一个可以优化的快速代码:
for f in directory
do
isJPEG=$(file $f | grep JPEG)
if [ -n "$isJPEG" ]; then
echo $f
fi
done
这个怎么样?
PROMPT> ls * | sed 's/^.*$/"&"/g' | xargs file
IREffectFilter.m: ASCII C++ program text, with CRLF line terminators
MacBook Air.spx: XML document text
PGFilteredImageView.m: ASCII C++ program text
Screen Shot 2012-09-27 at 1.33.50 PM.png: PNG image data, 559 x 152, 8-bit/color RGB, non-interlaced
Screen Shot 2012-10-02 at 10.04.37 AM.png: PNG image data, 880 x 1136, 8-bit/color RGBA, non-interlaced
Screen Shot 2012-10-02 at 2.08.53 PM.png: PNG image data, 937 x 1158, 8-bit/color RGB, non-interlaced
Screen Shot 2012-10-03 at 9.50.16 AM.png: PNG image data, 651 x 347, 8-bit/color RGB, non-interlaced
Screen Shot 2012-10-08 at 11.14.51 AM.png: PNG image data, 1343 x 1528, 8-bit/color RGBA, non-interlaced
Screen Shot 2012-10-08 at 12.09.01 PM.png: PNG image data, 880 x 1136, 8-bit/color RGBA, non-interlaced
Screen Shot 2012-10-08 at 12.29.16 PM.png: PNG image data, 880 x 1136, 8-bit/color RGBA, non-interlaced
Screen Shot 2012-10-09 at 8.44.05 AM.png: PNG image data, 880 x 1136, 8-bit/color RGBA, non-interlaced
Screen Shot 2012-10-11 at 4.17.14 PM.png: PNG image data, 880 x 1136, 8-bit/color RGBA, non-interlaced
breakpoint1.sh: Bourne-Again shell script text executable
breakpoint2.sh: Bourne-Again shell script text executable
bugtracker.txt: UTF-8 Unicode text
error_svn_rename.txt: UTF-8 Unicode English text
long_text.txt: data
尝试ls | grep -v '.' |file -f -
. 例如,在显示ls
的目录中
2 ga() ab cd date.txt ga() mo times x?gh
和file *
节目
2 ga() : ASCII text, with very long lines
ab cd: empty
date.txt: ASCII text
ga() mo: PDF document, version 1.5
times: HTML document, ASCII text
x?gh: JPEG image data, JFIF standard 1.01
命令管道ls | grep -v '.' |file -f -
显示
2 ga() : ASCII text, with very long lines
ab cd: empty
ga() mo: PDF document, version 1.5
times: HTML document, ASCII text
x?gh: JPEG image data, JFIF standard 1.01
(显然file -
在这个例子中输出了行内的额外制表符或空格。 请注意,第一个文件名的末尾有两个空格,如冒号前的空格所示。 另请注意,命令ls | grep -v '.' |file -f - |grep -i jpeg
产生:
x?gh: JPEG image data, JFIF standard 1.01