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.

Recommended Answers

All 2 Replies

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

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.