Bash Script: Files and Folders

SpoonTech

Well-Known Member
Joined
Jan 19, 2011
Messages
360
Reaction score
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.

ie. Make new folder P100 if folder doesn't exist, move file P100_DEFAULT.jpg into this folder.

Code:
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.
 
Code:
#!/bin/bash -x
for file in *.jpg
do
   A=`echo ${file}|cut -d. -f1`
   B=`echo ${file}|cut -d. -f2`
   NewF="${A}_DEFAULT.${B}"
   if [ ! -d "${A}" ]; then mkdir ${A}; fi
   mv ${file} ${A}/${NewF}
done
Keep the "-x" for debug. Remove if everything works. Append "echo" before the commands to test the correct output before running on real files.
 
#The above would work but I figure we can use less lines:
Code:
for FILE in *.jpg
do
  SUBDIR=`echo $FILE | cut -d'.' -f1`
  [ -d $SUBDIR ] || mkdir $SUBDIR
  mv $FILE $SUBDIR/`echo $FILE | sed 's/\./_DEFAULT\./g'`
done
 
Last edited:
is it possibly to install a bash client in windows, such that i could use bash in command prompt?
 
Hi All,

Thanks for the input! Seems the same thing can be solved in many different ways.
I landed up doing it like this:

Code:
#!/bin/bash

for file in *.jpg; do
    mkdir ${file%.jpg}
    mv $file ${file%.jpg}/${file%.jpg}_DEFAULT.jpg
done

The only thing is that I cannot double click it and run it, but I'm sure I could make a plan with this in a few mins of work. Will give it a go when I get time. At the moment, I am clicking on the folder, and saying 'New Terminal in Folder', and then typing 'bash RN.sh'.

I could correct this by adding:
Code:
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

to get the directory, however then this changes the $file variable.
Perhaps nod and gdot can post explantations to the code.

An interesting thing to note is that when I used "echo mv $file ${file%.jpg}/${file%.jpg}_DEFAULT.jpg" the script did not work properly, however when I removed the echo it worked fine!
 
To be honest, I'm not familiar with the ${file%.jpg} of specifying variables. both me and nod assumed you wanted to make the directory name=filename and not=filename.jpg. But if we're making it equal to filename.jpg, you can save a line of code.

I tested my previous script on my mac. Let me change it and add some comments:
Code:
#for each file in current directory
for FILE in *.jpg
do
  #if a folder with the same name does not exist, create it
  [ -d $FILE ] || mkdir $FILE
  #move the file into the subdirectory with the same name but
  # rename it with _DEFAULT before the .
  mv $FILE $FILE/`echo $FILE | sed 's/\./_DEFAULT\./g'`
done

As for an icon to click, look into using automator. I don't have my mac with me right now but I'm pretty sure you can set up an automator workflow pretty easily for this.
 
Last edited:
You could try:
Code:
#!/bin/bash
WORKDIR=/dir/with/jpegs

cd ${WORKDIR}
for file in *.jpg; do
    mkdir ${file%.jpg}
    mv $file ${file%.jpg}/${file%.jpg}_DEFAULT.jpg
done

To echo the command write it as:
Code:
#!/bin/bash
WORKDIR=/dir/with/jpegs

cd ${WORKDIR}
for file in *.jpg; do
    echo "mkdir ${file%.jpg}"
    echo "mv $file ${file%.jpg}/${file%.jpg}_DEFAULT.jpg"
done

Remember to "chmod 750 <scriptname>". You should then be able to double-click on it.
 
@Nod, you cannot cd within bash!
Code:
#!/bin/bash
WORKDIR=/home/jna/Pictures
echo -e "Before cd ... \c "; pwd
cd ${WORKDIR}
echo -e "After cd ... \c "; pwd
Result:
Code:
# ./t.sh
Before cd ... /home/jna
After cd ... /home/jna/Pictures
Why not just try it with the echo statements first?
 
Top
Sign up to the MyBroadband newsletter
X