[mdlug] batch name changes

Ingles, Raymond Raymond.Ingles at compuware.com
Fri Jan 25 12:37:07 EST 2013


> From: Garry Stahl

> I understood not a word of that.  I have no idea how to put it into
> practice.

It's a shell script. Linux tends to use combinations of lots of little tools - rather than large, monolithic, all-in-one programs - to get things done. In this case, we're using the 'file' command to identify image files, then picking what type of image file it is, and using the 'mv' command to rename them with an extension. (And we're also using the 'cut' utility to pick out the specific info we want from the 'file' command's output.)

If you saved the text into a file like "image_extender.sh" and put it in your home directory, then you could combine it with the unix 'find' command. For example, move to the top level of the image directory, and run:

 find . -name "*" -exec ~/image_extender.sh '{}' \;

This will find every file, and pass it to image_extender.sh, which will confirm it's actually an image and give it the appropriate extension. Right now it only handles GIF and JPEG, but you could add more types. For example, you could add lines like:

  TIFF)
    echo "It's a TIFF!"
    mv $FILE $FILE.tiff
  ;;

...in the appropriate spot in the script.

> > #!/bin/sh
> >
> > # Helps handle filenames with spaces
> > IFS=`printf '\n\t'`
> >
> > for FILE in $*
> > do
> >   echo $FILE
> >   IS_IMAGE=`file -b $FILE | cut -f2 -d" "`
> >   if [ "image" = "$IS_IMAGE" ]; then
> >     IMAGE_TYPE=`file -b $FILE | cut -f1 -d" "`
> >     case $IMAGE_TYPE in
> >      GIF)
> >        echo "It's a gif!"
> >        mv $FILE $FILE.gif
> >      ;;
> >      JPEG)
> >        echo "It's a jpeg!"
> >        mv $FILE $FILE.jpg
> >      ;;
> >      *)
> >        echo "I dunno what to do with image type " $IMAGE_TYPE
> >      ;;
> >     esac
> >   fi
> > done




More information about the mdlug mailing list