0

Hi,

I am trying to write a Bash Script to sort some images but I am new to Bash and not having any luck.
I have a folder of images.

Example:
P100.jpg
P172.jpg
P342.jpg
P400.png

I need to change these filenames to <filename>_DEFAULT<extension>.
ie. P100_DEFAULT.jpg and then move the file into a folder with the name of the original file.

Pseudo:

    Load file list for directory

    For each file in the directory:
    {
        filename = <filename>
        newfilename = <filename>_DEFAULT<extension>
        mkdir <filename> #if directory doesn't exist
        mv <filename> <newdir/newfilename>
    }

NOTE: I will be running the script on MacOSX (BASH 3.2)

Any help will be appreciated.

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by histrungalot
0

what you mean change the file name and copy in a folder
are you want to a move copy of your file with a new name and move in another folderor
change the orignal file extention and rename
explain

0

See if this helps

#!/bin/bash
if [ -d $1 ]
then
  cd $1
  for file in $(ls *.jpg)
  do
     newfile=${file%.jpg}"_DEFAULT"${file:(-4)}
     echo "New file name: $newfile"
  done
else
  echo "Directory $1 doesn't exist"
fi

Testing

$ ls ./temp
P100.jpg    P172.jpg    P342.jpg    P400.jpg
$ ./myScript temp/
New file name: P100_DEFAULT.jpg
New file name: P172_DEFAULT.jpg
New file name: P342_DEFAULT.jpg
New file name: P400_DEFAULT.jpg
$

Just add the rest

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.