Automator :: Remove 6 digit date from filename; append current date
This should do as you requested:
#!/bin/bash
today=$(date +%y%m%d)
for f in "$@"; do
filebasename=${f##*/}
filename=${filebasename%.*}
path=${f%/*}
ext=${f##*.}
if [[ "$filename" =~ ^.*_[0-9]{6}$ ]]; then
filename="$(sed -E -e 's/_[0-9]{6}$//'<<<"$filename")"
newfilename="$filename"_"$today"
mv "$f" "$path"/"$newfilename"."$ext"
else
newfilename="$filename"_"$today"
mv "$f" "$path"/"$newfilename"."$ext"
fi
done
Note that when used within a Run Shell Script action in an Automator workflow, you may need to add the full path to sed
in the code provided, e.g.: /usr/bin/sed