Skip to content

Instantly share code, notes, and snippets.

@justjanne
Last active July 17, 2019 18:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justjanne/c7b9bfd2780c4267ba4f1f870994917a to your computer and use it in GitHub Desktop.
Save justjanne/c7b9bfd2780c4267ba4f1f870994917a to your computer and use it in GitHub Desktop.
#!/bin/bash
declare -A fileinfo
# Resets the fileinfo map
fileinfo_reset() {
fileinfo[year]="0000"
fileinfo[month]="00"
fileinfo[day]="00"
fileinfo[hour]="00"
fileinfo[minute]="00"
fileinfo[second]="00"
fileinfo[sequence]=""
fileinfo[extension]="invalid"
fileinfo[format]="invalid"
}
# Populates the fileinfo map with the attributes parsed from the filename
# Returns 1 if it can't process the file
fileinfo_match() {
# Capture groups
# 1 2 3 4 5 6 7 8 9 10 11 12 13 17 18
if [[ $1 =~ ^(Screenshot\ from\ |Bildschirmfoto\ am\ |Screenshot_|IMG_|JPEG_|MVIMG_|VID_)?([0-9]{4})(_|-|\ |\.)?([0-9]{2})(_|-|\ |\.)?([0-9]{2})(_|-|\ |\.|\ um\ )?([0-9]{2})(%3a|:|\.|-|\ )?([0-9]{2})(%3a|:|\.|-|\ )?([0-9]{2})((_|\.)?[0-9]{3})?((-|~|_|\.|\ \()(.+)\)?)*.(png|jpg|jpeg|mp4)$ ]]; then
fileinfo[year]="${BASH_REMATCH[2]}"
fileinfo[month]="${BASH_REMATCH[4]}"
fileinfo[day]="${BASH_REMATCH[6]}"
fileinfo[hour]="${BASH_REMATCH[8]}"
fileinfo[minute]="${BASH_REMATCH[10]}"
fileinfo[second]="${BASH_REMATCH[12]}"
fileinfo[sequence]="${BASH_REMATCH[17]}"
fileinfo[extension]="${BASH_REMATCH[18]}"
fileinfo[format]="${BASH_REMATCH[1]}1970${BASH_REMATCH[3]}01${BASH_REMATCH[5]}01${BASH_REMATCH[7]}00${BASH_REMATCH[9]}00${BASH_REMATCH[11]}00.${extension}"
return 0
# Capture groups
# 1 2 3
elif [[ $1 =~ ^(Screenshot\ from\ |Screenshot_|IMG_|JPEG_|MVIMG_|VID_)?([0-9]+).(png|jpg|jpeg|mp4)$ ]]; then
fileinfo[year]="$(date -d @${BASH_REMATCH[2]} +%Y)"
fileinfo[month]="$(date -d @${BASH_REMATCH[2]} +%m)"
fileinfo[day]="$(date -d @${BASH_REMATCH[2]} +%d)"
fileinfo[hour]="$(date -d @${BASH_REMATCH[2]} +%H)"
fileinfo[minute]="$(date -d @${BASH_REMATCH[2]} +%M)"
fileinfo[second]="$(date -d @${BASH_REMATCH[2]} +%S)"
fileinfo[extension]="${BASH_REMATCH[3]}"
fileinfo[format]="${BASH_REMATCH[1]}1562852834.${extension}"
return 0
else
return 1
fi
}
# Builds the new filename from the fileinfo map
fileinfo_name() {
if [ "${fileinfo[sequence]}" != "" ]; then
echo "${fileinfo[year]}${fileinfo[month]}${fileinfo[day]}_${fileinfo[hour]}${fileinfo[minute]}${fileinfo[second]}-${fileinfo[sequence]}.${fileinfo[extension]}"
else
echo "${fileinfo[year]}${fileinfo[month]}${fileinfo[day]}_${fileinfo[hour]}${fileinfo[minute]}${fileinfo[second]}.${fileinfo[extension]}"
fi
}
# Parsing parameters
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-a|--apply)
apply=1
shift # past argument
;;
-d|--debug)
debug=1
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
# Printing header for simulated run
if [ ! $apply ]; then
echo "Proposed Changes:"
fi
# Use positional parameters as targets, otherwise current folder
if [ ${#POSITIONAL[@]} -eq 0 ]; then
targets=($PWD)
else
targets=$POSITIONAL
fi
# Processing files
find "${targets[@]}" -maxdepth 1 -type f -name "*.png" -o -name "*.jpg" -o -name "*.jpeg" -o -name "*.mp4" | while read filename
do
fileinfo_reset
valid=0
old=$(basename "${filename}")
directory=$(dirname "${filename}")
if fileinfo_match "$old"; then
valid=1
else
echo " Invalid format: ${filename}"
fi
if [ $debug ]; then
for key in "${!fileinfo[@]}"; do
echo -n "${key}=${fileinfo[${key}]}, "
done
echo
fi
# Only process files if we can find a valid format
if (($valid)); then
new=$(fileinfo_name)
# Only process files with changed names
if [ "$new" != "$old" ]; then
if [ $apply ]; then
mv "${directory}/${old}" "${directory}/${new}"
else
echo " mv ${directory}/${old}" "${directory}/${new}"
fi
fi
fi
done
# Printing footer for simulated run
if [ ! $apply ]; then
echo "Use ./sanitize.sh --apply to apply the changes"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment