It can be achieved on the command line, but a script that would run on the command line would be an easier solution (I think).
Basic steps:
Get a list of directories to iterate over:
find ${directory} -mindepth 1 -type d
Check each directory for the presence of config.ini
, and image.jpg
.
if [ -f ${subdir}/config.ini -a -f ${subdir}/image.jpg ]; then ...
- Check the config.ini for all the right parts of the timestamp.
various grep ^Year= ${subdir}/config.ini
or ^Month
, etc...
- Make a copy of the image.jpg file, using the timestamp.
cp ${subdir}/image.jpg ${copydir}/${timestamp}.jpg
I think it's easier, and potentially safer to put these sequences into a script, where you can more easily put in readable output, error handling, etc.
Here's an example script to do those steps:
#!/bin/bash
imagepath="/path/to/images"
copydir="/path/to/copies"
# step 1: find all the directories
for dir in $(find ${imagepath} -mindepth 1 -type d); do
echo "Procesing directory $dir:"
ci=${dir}/config.ini
jp=${dir}/image.jpg
# step 2: check for config.ini and image.jpg
if [ -f ${ci} -a -f ${jp} ]; then
# step 3: get the parts of the timestamp
year=$(grep ^Year= ${ci} | cut -d= -f2)
month=$(grep ^Month= ${ci} | cut -d= -f2)
day=$(grep ^Day= ${ci} | cut -d= -f2)
hour=$(grep ^Hour= ${ci} | cut -d= -f2)
min=$(grep ^Minute= ${ci} | cut -d= -f2)
sec=$(grep ^Second= ${ci} | cut -d= -f2)
ms=$(grep ^Milliseconds= ${ci} | cut -d= -f2)
# if any timestamp part is empty, don't copy the file
# instead, write a note, and we can check it manually
if [[ -z ${year} || -z ${month} || -z ${day} || -z ${hour} || -z ${min} || -z ${sec} || -z ${ms} ]]; then
echo "Date variables not as expected in ${ci}!"
else
# step 4: copy file
# if we got here, all the files are there, and the config.ini
# had all the timestamp parts.
tsfile="${year}-${month}-${day}T${hour}:${min}:${sec}:${ms}.jpg"
target="${copydir}/${tsfile}"
echo -n "Archiving ${jp} to ${target}: "
st=$(cp ${jp} ${target} 2>&1)
# capture the status and alert if there's an error
if (( $? == 0 )); then
echo "[ ok ]"
else
echo "[ err ]"
fi
[ ! -z $st ] && echo $st
fi
else
# other side of step2... some file is missing...
# manual check recommended, no action taken
echo "No config.ini or image.jpeg in ${dir}!"
fi
echo "---------------------"
done
It's always good to be somewhat conservative with scripts like this, so you don't accidentally delete files. This script only does 1 copy action, so that's pretty conservative, and it shouldn't harm your source files. But you may want to change specific actions or output messages to better suit your needs.