如何检查文件夹中的所有文件是否具有相同的格式

时间:2012-10-11 16:42:52

标签: linux bash shell

我有一个包含文件列表的文件夹。问题是他们没有扩展。我想检查它们是JPEG还是JPG格式。我试过这个

find folder -type f -not -name "*.*"

输出

folder/t351
folder/t352
folder/t353
folder/t354
folder/t355

我尝试使用Regex,但由于文件的名称没有扩展名,所以没有用。 有人能告诉我如何验证文件并检查它们是否具有某种格式。

3 个答案:

答案 0 :(得分:1)

这个怎么样?

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

答案 1 :(得分:1)

您应该使用file命令。 我建议你一个快速的代码,你可以改进:

for f in directory
do
    isJPEG=$(file $f | grep JPEG)
    if [ -n "$isJPEG" ]; then
       echo $f
    fi
done

答案 2 :(得分:1)

试试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