[Solved] Bash to Sort Photos (Complete Newbie)

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Kiyonari

[Solved] Bash to Sort Photos (Complete Newbie)

Post by Kiyonari »

Hi guys,

I need a script to sort my recovered 6500ish photos into directories based on filename. They are all named by date i.e. 2007-08-17--09.29.04 15d.jpg so I would like to create and move into directories based on first 10 characters of filename (the date) like the picture below:

http://tinypic.com/r/8y56ow/8

I've scoured around various forums and come up with the following, though it does nothing just gives synax error :(

for filename in *.*; do
dir_name=${filename:0:7};
mkdir -p $dir_name
mv -i $filename $dir_name/${filename}
fi
done

I have no clue what I am doing!
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Kiyonari

Re: Bash to Sort Photos (Complete Newbie)

Post by Kiyonari »

Correction:

for filename in *.*; do
dir_name=${filename:0:10};
mkdir -p $dir_name
mv -i $filename $dir_name/${filename}
fi
done
Kiyonari

Re: Bash to Sort Photos (Complete Newbie)

Post by Kiyonari »

for filename in *.*; do
dir_name=${filename:0:7};
mkdir -p $dir_name
mv -i $filename $dir_name/${filename}
done

Sorry now removed fi as no if
Habitual

Re: Bash to Sort Photos (Complete Newbie)

Post by Habitual »

use

Code: Select all

set -x
in the script or just before you run the code manually?
What errors does it then show?
User avatar
Flemur
Level 20
Level 20
Posts: 10096
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: Bash to Sort Photos (Complete Newbie)

Post by Flemur »

though it does nothing just gives synax error

Extraneous 'fi' ('sposed to go after an 'if').

"7" should also be "10"...?
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
Kiyonari

Re: Bash to Sort Photos (Complete Newbie)

Post by Kiyonari »

Thanks for the replies.

The following errors occur:

http://pastebin.com/M565n2Py

I think it might be because each of the filenames has a space prior to the unique number I gave them. Is there a way around this without renaming again? The script makes each directory perfectly, just doesn't move any files. :mrgreen:
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Bash to Sort Photos (Complete Newbie)

Post by Pilosopong Tasyo »

There are two problems with the mv command in your script:
  1. The source files doesn't exist.
  2. The destination directory doesn't exist either.
One of the syntaxes of the mv command goes like this:

Code: Select all

mv source1 source2 source3 ... destination
So, using the example from your pastebin:

Code: Select all

mv -i 2012-03-13--14.31.34 471d.jpg 2012-03-13/2012-03-13--14.31.34 471d.jpg
source1 = 2012-03-13--14.31.34
source2 = 471d.jpg
source3 = 2012-03-13/2012-03-13--14.31.34
destination = 471d.jpg

Remember: commands use whitespace (tabs, spacebar) to separate parameters just as we use spaces to separate words.

So, to fix this problem, enclose variables that contain text with spaces in "double quotation marks" (especially filenames) so they get treated as one unit:

Code: Select all

mv -i "2012-03-13--14.31.34 471d.jpg" "2012-03-13/2012-03-13--14.31.34 471d.jpg"
source = 2012-03-13--14.31.34 471d.jpg
destination = 2012-03-13/2012-03-13--14.31.34 471d.jpg

And for the particular line of text that generated the error message:

Code: Select all

mv -i "$filename" $dir_name
Assuming the $dir_name will always contain numbers and hyphens, there's no need to enclose it in double quotation marks. Also, no need to specify $filename after the $dir_name. You are just moving the file with its filename intact, are you not? If that's the case, just specify the directory as destination.

HTH.
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
Kiyonari

Re: Bash to Sort Photos (Complete Newbie)

Post by Kiyonari »

Thats correct Pilosopong, I'll make the tweaks and give it a shot, thanks!! :D
Kiyonari

Re: Bash to Sort Photos (Complete Newbie)

Post by Kiyonari »

Worked perfectly, what an awesome community! Thanks again.
Locked

Return to “Scripts & Bash”