Skip to content

Instantly share code, notes, and snippets.

@davidlj95
Last active September 7, 2023 17:31
Show Gist options
  • Save davidlj95/938829a9407c8b0c669196b1fc675a66 to your computer and use it in GitHub Desktop.
Save davidlj95/938829a9407c8b0c669196b1fc675a66 to your computer and use it in GitHub Desktop.
File with dates renamer. From (YYYY.MM.DD). From YYYYMMDD. To YYYY-MM-DD. Replaces _ with -
#!/bin/sh
[ $# -eq 0 ] && >&2 echo "❌ No files provided as arguments" && exit 1
NOSPACES_FILE_REGEXP="^([0-9]{4})([0-9]{2})([0-9]{2})(.*)$"
PARENTHESES_FILE_REGEXP="^\(([0-9]{4})\.([0-9]{2})\.([0-9]{2})\)(.*)$"
for filename in "$@";
do
new_filename="$filename"
printf "\"$filename\""
# No spaces
# Testing starts with 19|20 to ensure a bit more it's a year
if echo "$filename" | egrep -q "^(19|20)"; then
new_filename="$(echo "$new_filename" | sed -E "s/${NOSPACES_FILE_REGEXP}/\1-\2-\3\4/g")"
fi
# Parentheses
new_filename="$(echo "$new_filename" | sed -E "s/${PARENTHESES_FILE_REGEXP}/\1-\2-\3\4/g")"
# Undescores to dashes
new_filename="$(echo "$new_filename" | tr '_' '-')"
# Output diff
if [ "$new_filename" == "$filename" ]; then
echo " (no change)"
else
echo " -> \"$new_filename\""
mv "$filename" "$new_filename"
fi
done
@davidlj95
Copy link
Author

RESERVED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment