Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bash: automatic file extension

0 views
Skip to first unread message

Florian Reiser

unread,
Aug 24, 2004, 2:52:43 PM8/24/04
to
Hello,

i am trying to figure out how to solve the following problem:

I have a directory with lots of pictures, but none of them have any file
extensions. There are different formats, actually only .jpg and .bmp

Is there a way to automatically add the missing file extensions with a
script ? There is the command "file" which gives out what kind of format a
file has, for instance ...

florian@linux: /data> file image01
image01: JPEG image data, EXIF standard 0.73, 10752 x 2048

Something like this might work for me ...

#Script
for datei in *
do
if
file $datei="JPEG"*
then
extension=".jpeg"
else
extension=".bmp"
fi
done

I`ve tried the script but it does not work - i can`t get the
if file $datei="JPEG"*
part to work.
Maybe there`s a more simple way ?

Thanks for any help,

Florian

Kenny McCormack

unread,
Aug 24, 2004, 3:13:50 PM8/24/04
to
In article <cgg2m9$i0o$00$1...@news.t-online.com>,

Florian Reiser <c...@reiser-familie.de> wrote:
>Hello,
>
>i am trying to figure out how to solve the following problem:
>
>I have a directory with lots of pictures, but none of them have any file
>extensions. There are different formats, actually only .jpg and .bmp
>
>Is there a way to automatically add the missing file extensions with a
>script ? There is the command "file" which gives out what kind of format a
>file has, for instance ...
>
>florian@linux: /data> file image01
>image01: JPEG image data, EXIF standard 0.73, 10752 x 2048
>
>Something like this might work for me ...
>
>#Script
>for datei in *
>do
> if
> file $datei="JPEG"*
> then
> extension=".jpeg"
> else
> extension=".bmp"
>fi
>done

This is tailor made for the case statement.

case `file $i` in
*JPEG*) ext="jpg";;
*GIF*) ext="gif";;
...
esac

I didn't have a BMP file on my Unix system to test with, but you should be
able to get that part working...

Florian Reiser

unread,
Aug 24, 2004, 3:34:28 PM8/24/04
to
> This is tailor made for the case statement.
Very nice, i haven`t gotten to that one yet ... it was just what i was
looking for.

> I didn't have a BMP file on my Unix system to test with, but you should be
> able to get that part working...

It`s working ! Thanks a lot !!!

Florian

William Park

unread,
Aug 24, 2004, 5:16:59 PM8/24/04
to
Florian Reiser <c...@reiser-familie.de> wrote:
> Hello,
>
> i am trying to figure out how to solve the following problem:
>
> I have a directory with lots of pictures, but none of them have any file
> extensions. There are different formats, actually only .jpg and .bmp
>
> Is there a way to automatically add the missing file extensions with a
> script ? There is the command "file" which gives out what kind of format a
> file has, for instance ...
>
> florian@linux: /data> file image01
> image01: JPEG image data, EXIF standard 0.73, 10752 x 2048
>
> Something like this might work for me ...
>
> #Script
> for datei in *
> do
> if
> file $datei="JPEG"*

if file $datei | grep JPEG >/dev/null; then
...
fi
or
case `file $datei` in
*JPEG*) ... ;;
esac

> then
> extension=".jpeg"
> else
> extension=".bmp"
> fi
> done
>
> I`ve tried the script but it does not work - i can`t get the
> if file $datei="JPEG"*
> part to work.
> Maybe there`s a more simple way ?

You can also test more than one file at a time:
file * | grep JPEG | awk -F : '{print $1}'

--
William Park <openge...@yahoo.ca>
Open Geometry Consulting, Toronto, Canada

Ed Morton

unread,
Aug 24, 2004, 6:23:16 PM8/24/04
to

William Park wrote:
<snip>


> You can also test more than one file at a time:
> file * | grep JPEG | awk -F : '{print $1}'
>

No need for both grep AND awk:

file * | awk -F : '/JPEG/{print $1}'

Ed.

0 new messages