We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,949 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

File Renaming & Folder Sorting

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.

2
Contributors
1
Reply
1 Month
Discussion Span
1 Year Ago
Last Updated
3
Views
John Linux
Light Poster
29 posts since May 2010
Reputation Points: 30
Solved Threads: 0
Skill Endorsements: 0
for FILE in *; do
    NEWFILE="${FILE%.*}_DEFAULT.${FILE##*.}"
    mv "$FILE" "$NEWFILE"
    mkdir -p "$FILE"
    mv "$NEWFILE" "$FILE"
done
KenJackson
Newbie Poster
13 posts since Apr 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0575 seconds using 2.66MB