Skip to content

Instantly share code, notes, and snippets.

@larryhou
Last active December 26, 2016 06:06
Show Gist options
  • Select an option

  • Save larryhou/09f04bf8884c0837dea3890988d3486e to your computer and use it in GitHub Desktop.

Select an option

Save larryhou/09f04bf8884c0837dea3890988d3486e to your computer and use it in GitHub Desktop.
Group photo by year, and rename photo with continuous index automatically
#!/bin/bash
find . \( -iname '*?.*' -not -iname '*.sh' -not -iname '*.txt' \) -maxdepth 1 > new.txt
find . \( -iname '*?.*' -not -iname '*.sh' -not -iname '*.txt' \) -mindepth 2 > raw.txt
BASE_TIME=$(cat new.txt | xargs stat -f '%B' | sort -n | head -n 1 | awk '{print $1}')
if [ "${BASE_TIME}" = "" ]
then
echo "WARN:NO_PICTURES_FOR_PROCESS"
rm *.txt
exit
fi
cat raw.txt | xargs stat -f '%B|%N' | awk -F'|' -v time=${BASE_TIME} '{if($1<time){print $2}}' > old.txt
BASE_INDX=$(cat old.txt | sort -n | tail -n 1 | awk -F'_' '{print $NF}' | awk -F'.' '{print $1}')
if [ "${BASE_INDX}" = "" ]
then
BASE_INDX=0
fi
echo "BASE_TIME:${BASE_TIME} BASE_INDX:${BASE_INDX}"
cat raw.txt | xargs stat -f '%B|%N' | awk -F'|' -v time=${BASE_TIME} '{if($1>=time){print $2}}' > old.txt
function dojob()
{
mkdir -pv ${2}
name=$(uuidgen).$(echo ${3} | awk -F'/' '{print $NF}')
mv -v ${3} ${name}
id=$(python -c "print '%05d'%(${4})")
mv -v ${name} ${2}/IMG_${id}.$(echo ${3}|awk -F'.' '{print $NF}')
}
export -f dojob
cat new.txt old.txt | xargs stat -t '%Y' -f '%B|%SB|%N' | sort -n | awk -F'|' -v offset=${BASE_INDX} '{system("dojob " $1" "$2" "$3" "NR+offset)}'
rm *.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment