0

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

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

输出是

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

我尝试使用正则表达式,但由于文件名没有扩展名,所以它不起作用。有人可以告诉我如何验证文件并检查它们是否具有某种格式。

4

3 回答 3

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
于 2012-10-11T16:55:55.560 回答
1

您应该使用文件命令。我向您推荐一个可以改进的快速代码:

for f in directory
do
    isJPEG=$(file $f | grep JPEG)
    if [ -n "$isJPEG" ]; then
       echo $f
    fi
done
于 2012-10-11T17:00:55.420 回答
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
于 2012-10-11T19:34:30.383 回答