[Solved] Bash to Sort Photos (Complete Newbie)

Forum rules
Before you post please read how to get help
Kiyonari
Level 1
Level 1
Posts: 18
Joined: Thu Mar 06, 2014 2:07 pm

[Solved] Bash to Sort Photos (Complete Newbie)

Postby Kiyonari » Fri Mar 14, 2014 2:56 pm

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 Kiyonari on Mon Mar 17, 2014 9:53 am, edited 1 time in total.

Kiyonari
Level 1
Level 1
Posts: 18
Joined: Thu Mar 06, 2014 2:07 pm

Re: Bash to Sort Photos (Complete Newbie)

Postby Kiyonari » Fri Mar 14, 2014 2:57 pm

Correction:

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

Kiyonari
Level 1
Level 1
Posts: 18
Joined: Thu Mar 06, 2014 2:07 pm

Re: Bash to Sort Photos (Complete Newbie)

Postby Kiyonari » Fri Mar 14, 2014 3:03 pm

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

User avatar
Habitual
Level 12
Level 12
Posts: 4337
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio
Contact:

Re: Bash to Sort Photos (Complete Newbie)

Postby Habitual » Fri Mar 14, 2014 3:43 pm

use

Code: Select all

set -x
in the script or just before you run the code manually?
What errors does it then show?
Image

User avatar
Flemur
Level 10
Level 10
Posts: 3268
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: Bash to Sort Photos (Complete Newbie)

Postby Flemur » Fri Mar 14, 2014 4:16 pm

though it does nothing just gives synax error

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

"7" should also be "10"...?
Mint 18 Mate/fluxbox/pulse-less - Xubuntu 16.04/fluxbox/pulse-less
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?

Kiyonari
Level 1
Level 1
Posts: 18
Joined: Thu Mar 06, 2014 2:07 pm

Re: Bash to Sort Photos (Complete Newbie)

Postby Kiyonari » Mon Mar 17, 2014 5:44 am

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: 1376
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Bash to Sort Photos (Complete Newbie)

Postby Pilosopong Tasyo » Mon Mar 17, 2014 6:19 am

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.
Image
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
Level 1
Level 1
Posts: 18
Joined: Thu Mar 06, 2014 2:07 pm

Re: Bash to Sort Photos (Complete Newbie)

Postby Kiyonari » Mon Mar 17, 2014 9:01 am

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

Kiyonari
Level 1
Level 1
Posts: 18
Joined: Thu Mar 06, 2014 2:07 pm

Re: Bash to Sort Photos (Complete Newbie)

Postby Kiyonari » Mon Mar 17, 2014 9:52 am

Worked perfectly, what an awesome community! Thanks again.


Return to “Scripts & Bash”

Who is online

Users browsing this forum: No registered users and 1 guest