[Solved] Bash to Sort Photos (Complete Newbie)
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
[Solved] Bash to Sort Photos (Complete Newbie)
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!
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.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: Bash to Sort Photos (Complete Newbie)
Correction:
for filename in *.*; do
dir_name=${filename:0:10};
mkdir -p $dir_name
mv -i $filename $dir_name/${filename}
fi
done
for filename in *.*; do
dir_name=${filename:0:10};
mkdir -p $dir_name
mv -i $filename $dir_name/${filename}
fi
done
Re: Bash to Sort Photos (Complete Newbie)
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
dir_name=${filename:0:7};
mkdir -p $dir_name
mv -i $filename $dir_name/${filename}
done
Sorry now removed fi as no if
Re: Bash to Sort Photos (Complete Newbie)
use
in the script or just before you run the code manually?
What errors does it then show?
Code: Select all
set -x
What errors does it then show?
Re: Bash to Sort Photos (Complete Newbie)
though it does nothing just gives synax error
Extraneous 'fi' ('sposed to go after an 'if').
"7" should also be "10"...?
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?
Your data and OS are backed up....right?
Re: Bash to Sort Photos (Complete Newbie)
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.
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.
- Pilosopong Tasyo
- Level 6
- Posts: 1432
- Joined: Mon Jun 22, 2009 3:26 am
- Location: Philippines
Re: Bash to Sort Photos (Complete Newbie)
There are two problems with the mv command in your script:
So, using the example from your pastebin:
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:
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:
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.
- The source files doesn't exist.
- The destination directory doesn't exist either.
Code: Select all
mv source1 source2 source3 ... destination
Code: Select all
mv -i 2012-03-13--14.31.34 471d.jpg 2012-03-13/2012-03-13--14.31.34 471d.jpg
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"
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
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].
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
Re: Bash to Sort Photos (Complete Newbie)
Thats correct Pilosopong, I'll make the tweaks and give it a shot, thanks!!
Re: Bash to Sort Photos (Complete Newbie)
Worked perfectly, what an awesome community! Thanks again.