I apologize in advance if this has been answered but have performed a lengthy search in hope of a solution.

I have a working Automator app that appends the current six-digit date (YYMMDD) to the end of the file name:

  • Input: Filename.txt
  • Output: Filename_181212.txt

Current bash command:

today=$(date +%y%m%d)
for f in "$@"
    mv "$f" "$path"/"$newfilename"."$ext"

This is great but after using it regularly, I have realized it would be nice not only append the current date but first check to see if a date is already appended to the filename; if so, remove it and append the current date, otherwise simply append the current date.

There two scenarios:

  1. Filename contains any date (previous/current/future) in the 6-digit format described previously preceded by an underscore (Filename_181212.txt)
  2. Filename does not contain at 6-digit date format appended to it (Filename.txt)

I realize that the solution I am hoping for will remove any six digits and its preceding "_" if it exists, whether it is a date or not.

Is it possible to precede my current bash command with something like:

if any combination of numbers exist immediately before the file extension (".ext") AND contains exactly 6 digits AND is preceded by an underscore ("_")

then remove the underscore plus 6 digits AND run the bash command listed above

if not then run the bash command above

Summary I'm trying to alleviate manually removing dates that may exist on a file name before appending the current date to them. Some files may have dates and others may not.

  • Yes but it seems like in this case, the date command is irrelevant because only 6 digits (plus preceding underscore) need to be found regardless if it is a past, previous or future date. – Jeremy W Dec 12 '18 at 20:30
  • sorry, I deleted my comment realizing it is stupid. – Ruskes Dec 12 '18 at 20:45
  • The date in file name is not Date, just plain text (numbers) but we see it as date. So if you delete all numbers in file name will that work ? – Ruskes Dec 12 '18 at 20:51
  • I believe so but the "_" will also need to be removed because one will be added when the current date is appended. As a reminder, if the date doesn't exist, then nothing is removed; it simply gets passed to the command that appends the date (which I currently have). – Jeremy W Dec 12 '18 at 20:57
  • that is quite surgical process you are asking for. And just deleting file name and replacing if with date is not an option for you. – Ruskes Dec 12 '18 at 21:15

This should do as you requested:


today=$(date +%y%m%d)

for f in "$@"; do


    if [[ "$filename" =~ ^.*_[0-9]{6}$ ]]; then

        filename="$(sed -E -e 's/_[0-9]{6}$//'<<<"$filename")"

        mv "$f" "$path"/"$newfilename"."$ext"   


        mv "$f" "$path"/"$newfilename"."$ext"



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

  • Okay. For clarity, if I change the variable of 'basename' (and its reference in the 'filename' variable) to something such as 'filebasename' then there should not be a conflict if used directly in the terminal or command line? – Jeremy W Dec 13 '18 at 5:47
  • @Jeremy W, Yes, changing it to anything other then an existing command name is a good thing to do, 'filebasename' is fine or anything other then exactly "baesname" in this use case. I left it as you originally wrote it so you'd be able to more easily see what code I added to your existing code. I'll update it in my answer now the I know you are aware of it. Also, see the updated note. – user3439894 Dec 13 '18 at 8:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .