Skip to content

Instantly share code, notes, and snippets.

@romiguelangel
Last active July 23, 2018 01:15
Show Gist options
  • Select an option

  • Save romiguelangel/7256faa3115c266fe02de935aff08001 to your computer and use it in GitHub Desktop.

Select an option

Save romiguelangel/7256faa3115c266fe02de935aff08001 to your computer and use it in GitHub Desktop.
#!/bin/bash
COLOR_DEFAULT='\033[0m'
COLOR_SUCCESS='\033[0;32m'
COLOR_WARNING='\033[1;33m'
COLOR_ERROR='\033[0;31m'
ZERO_VALUES=($(seq 1 1 9)) # Array with values from 1 to 9
# Include "0" value prefix in the first ten values 01, 02, ...
getPrefix () {
if [[ "${ZERO_VALUES[@]}" =~ "${1}" ]]; then
prefix="0"
else
prefix=""
fi
}
renameFile () {
getPrefix $2
nameUpdated="$(date -r "$1" +"%Y%m%d%H%M%S")$prefix$2.jpg"
if [[ $1 == $nameUpdated ]]; then
printf "\nfile ${COLOR_WARNING}$1${COLOR_DEFAULT} <------> ${COLOR_WARNING}$nameUpdated${COLOR_DEFAULT}\n"
return
fi
printf "\nfile ${COLOR_ERROR}$1${COLOR_DEFAULT} -------> ${COLOR_SUCCESS}$nameUpdated${COLOR_DEFAULT}\n"
output=$(mv -v -n "$1" "$nameUpdated")
echo $output
# Increase counter in the new filename when cannot overwritte because duplicate dates
echo "${output}" | grep -q "not overwritten"
if [ $? -eq 0 ]; then
counter=$(($2+1))
renameFile "$1" $counter
fi;
}
# @todo: Check if there are images in the folder
# if [[ ! -e *.{jpg, jpeg, JPG, JPEG} ]]; then # bad !!
# printf "\n${COLOR_ERROR}error: directory without images${COLOR_DEFAULT}\n"
# exit
# fi
for filename in *.{jpg,jpeg,JPG,JPEG}; do
if [ -s "$filename" ]; then
renameFile "$filename" 1
fi
done
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment