Last active
September 7, 2023 17:31
-
-
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 -
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
RESERVED