ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Images
2007-11-21
work
home
2007-11-22
work
home
2007-11-23
work
home
2007-11-24
work
home
~~~~~~~~~~~~~~~~~
2008-11-23
work
home
2008-11-24
work
home
Etc..
What I want to do is write a little bash script that takes two dates and if I want the work or home folders then loops through the entire directory between the given dates and gets all the images from either the work or home folders, then copies those images into a temporary dir giving them a sequential number.
Any ideas? So far I have (and it's not much!)...
--------------------------
#!/bin/bash
# Input Date 1
echo -n "Input date from (YYYY-MM-DD) [ENTER]: "
read FROM_DATE
# Input Date 2
echo -n "Input date to (YYYY-MM-DD) [ENTER]: "
read TO_DATE
# Work or Home
echo -n "Work or Home (w or h) [ENTER]: "
read W_OR_H
# Loop through folders between date ranges
# Loop through images in folder
# Copy image to temp dir with incrementing name
--------------------------
Once you've got the dates you can increment FROM_DATE by one in a while loop, until it reaches TO_DATE + 1. For example:
Code:
while [ $FROM_DATE \< $(date -d "$TO_DATE 1 day" +%Y-%m-%d) ]
do
echo processing $FROM_DATE
% your commands here
FROM_DATE=$(date -d "$FROM_DATE 1 day" +%Y-%m-%d)
done
Note that the ASCII comparison is valid in this case, since you have the date in yyyymmdd format (with hyphen or any other characters in it). Also you can skip the echo command to prompt the user, by means of the -p option of read.
Tinkster, your approach is good, but only for dates belonging to the same month and year. If the start date is 2008-10-23 and the end date is 2008-11-02, it fails. I think some processing using the date command is always necessary.
At a quick glance, you could probably use something like the -mmin option.
Something like:
Code:
# should find all files in current dir (and its sub dirs) that
# are last modified between MIN_AGO_LOWER and MIN_AGO_UPPER *minutes* ago
find . -mmin +${MIN_AGO_LOWER} -mmin -${MIN_AGO_UPPER} -name '*.
Then I put the following script together (UNTESTED, no idea if it actually works, but its a start)
Code:
#!/bin/bash
#procs
date2stamp () {
date --utc --date "$1" +%s
}
stamp2date (){
date --utc --date "1970-01-01 $1 sec" "+%Y-%m-%d %T"
}
dateDiff (){
case $1 in
-s) sec=1; shift;;
-m) sec=60; shift;;
-h) sec=3600; shift;;
-d) sec=86400; shift;;
*) sec=86400;;
esac
dte1=$(date2stamp $1)
dte2=$(date2stamp $2)
diffSec=$((dte2-dte1))
if ((diffSec < 0)); then abs=-1; else abs=1; fi
echo $((diffSec/sec*abs))
}
# vars
LOWER_DATE=$1 # furthest away date
UPPER_DATE=$2 # closer date
TARGET_DIR=$3 # path to your home or work folder NO TRAILING SLASH, but PREFIX WITH ./ if under current dir
LOWER=$(dateDiff -m "${LOWER_DATE}" "now")
UPPER=$(dateDiff -m "${UPPER_DATE}" "now")
# you can change the first ./ to match whatever is the root directory of where you'll be looking
find ./ -mmin +${LOWER} -mmin -${UPPER} -iregex '.*\(\.jpg\|\.gif\|\.png\)' -not -iregex '${TGT_DIR}/\(.*\)' -exec cp {} ${TGT_DIR}/ \;
Call it like ./whatever_you_called_it.sh 2008-11-15 2008-11-20 ./BACKUPS
Again, no idea if it works (if it does, theres a bonus )....but it should give you some ideas.
Wow, I woke up to a whole bunch of responses! I was a little despondent last night as I hadn't yet seen any!
Thanks everyone, I will tinker and let everyone know the outcome (probably take me a week or so as I'm a lazy sod and never find time to program at home as the xbox steals it all!)
# Empty the temp dir
rm -Rf $TEMP_PATH
mkdir $TEMP_PATH
# Set count
COUNT=1
while [ $FROM_DATE \< $TO_DATE ]
do
echo processing $FROM_DATE/$W_OR_H
# Check the directory exists, if not skip to next
# Loop through all the images in the directory
COUNT=$(
ls $BASE_FOLDER/$FROM_DATE/$W_OR_H/*.jpg | {
while read FILE ; do
# Copy the current to the image to the temp directory with count as the name
#echo " Copy $FILE to $TEMP_PATH/$COUNT.jpg"
echo Copy $FILE to $TEMP_PATH/$COUNT.jpg >> $LOG_FILE
# Increment count
cp $FILE $TEMP_PATH/$COUNT.jpg
COUNT=$(($COUNT+1))
done
echo $COUNT
}
)
Yes, it does I'm running it on a Mac with OS X 10.5
I tried doing it other ways but that was the only way I could get it working. Plus the stupid variable scope issues with the counter inside the pipped ls while loop got me stumped for a while!
Only thing I'm trying to do no is while looping through the images adding a text string to the bottom of the image so that when I create the timelapse photo I can notice the days change as I'm so good at positioning the camera in the same place if I don't change the clothes I'm wearing I can't see the change!
Anyway thanks for all the help it's been real handy.... all I need to do now is get imagemagick to work!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.