yesh
yesh

Reputation: 2070

How to check if all the files in a folder are of the same format

I have a folder which contains a list of files. The problem is they dont have extensions. I want to check if they are JPEG or JPG format. I tried this

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

The output is

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

I tried using Regex but since the name of the files have no extensions It din't work out. Can someone tell me how I can validate the files and check if they have a certain format.

Upvotes: 0

Views: 212

Answers (3)

James Waldby - jwpat7
James Waldby - jwpat7

Reputation: 8711

Try ls | grep -v '\.' |file -f -. For example, in a directory where ls shows

  2 ga\(\)\    ab cd  date.txt  ga() mo  times  x?gh

and file * shows

  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

the command pipe
ls | grep -v '\.' |file -f -
shows

  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

(Apparently file - is outputting extra tabs or spaces within lines in this example. Note, the first file name has two spaces at its end, as seen by spaces before the colon.) Also note, the command
ls | grep -v '\.' |file -f - |grep -i jpeg
produced:

  x?gh:    JPEG image data, JFIF standard 1.01

Upvotes: 1

Bruno von Paris
Bruno von Paris

Reputation: 902

You should use the file command. I propose you a quick code that you can refine:

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

Upvotes: 1

neoneye
neoneye

Reputation: 52221

How about this?

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

Upvotes: 1

Related Questions