View Full Version : How hard can it be? Simple sorting script
Ratatoskr
9th August 2009, 02:55 AM
I have this little pet project, a simple wget command runs from cron every minute, fetches webcam.jpg from another server, renames it to cam_20090804_1210.jpg for the time when it was fetched (as in august the 4th, 2009, at 12:10).
Of course, this gives me 1440 pictures each day, and I think it would be best to sort it in folders, with year/month/date/hour.
I was thinking of making a simple script in perl or something that got the date from either the filename or the filedate, and checked to see if there was an existing folder, made it if not, and put the file in the appropriate folder.
Of course, I suck at programming, so does anybody here have any suggestions?
logical muse
9th August 2009, 03:11 AM
Without actually writing it for you, and assuming you want to improve your programming skills, I suggest you look up regular expressions. You'll find it's pretty easy to do.
Nick Bogaerts
9th August 2009, 06:32 AM
I suggest you get your cron job to put it in the right folder to start with, rather than sorting them afterwards.
Have a look at the man page for date, and a quick read over bash control structures (if and for in particular)
I've written up a dirty solution below (It'll go wrong if the minute changes between the script being called and finishing execution, but as long as you don't call it at 59 seconds passed the minute it should be fine)
#!/bin/sh
for folder in `date +'%Y %m %d %H'`
do
if [ ! -d $folder ]
then
mkdir $folder
fi
cd $folder
done
wget -O- http://my/webcam/url > cam_`date +%M`.jpg
ddt
9th August 2009, 07:01 AM
Have a look at the man page for date, and a quick read over bash control structures (if and for in particular)
No need for "for", as you're getting and storing only one picture at a time.
I've written up a dirty solution below (It'll go wrong if the minute changes between the script being called and finishing execution, but as long as you don't call it at 59 seconds passed the minute it should be fine)
Does that really matter? But the script can be done much easier. See the hidden part.
#!/bin/sh
for folder in `date +'%Y %m %d %H'`
do
if [ ! -d $folder ]
then
mkdir $folder
fi
cd $folder
done
wget -O- http://my/webcam/url > cam_`date +%M`.jpg
No need to do it one directory at a time; mkdir -p will create the whole path at once. No need to check if the directory exists even. :)
#!/bin/sh
folder="base/dir/for/pictures/$(date +'%Y/%m/%d/%H)"
mkdir -p "$folder"
cd "$folder"
wget -O- http://my/webcam/url > cam_$(date +%M).jpg
Ratatoskr
9th August 2009, 07:35 AM
Thanks guys :) If I knew it was so simple I wouldn't have asked :)
Nick Bogaerts
9th August 2009, 07:54 AM
If you hadn't asked, I wouldn't have known it was simpler than I thought.
© 2001-2009, James Randi Educational Foundation. All Rights Reserved.
vBulletin® v3.7.5, Copyright ©2000-2010, Jelsoft Enterprises Ltd.