Copying multiple files and appending time stamp before file extension

Hi,

I have multiple files that read:

Asa.txt
Bad.txt
Gnu.txt

And I want to rename them using awk to

Asa_ddmmyytt.txt and so on
...
If there is a single command or more efficient executable please share!

Thanks!

How are you generating the datestamp?

I don't know awk, but this snippet should work in bash. Please test first.

mydate=$(date +%d%m%y%H%M)
for file in *.txt
do
   mv "${file}" "${file%.txt}_${mydate}.txt"
done

I'm simply generating the datestamp using the current date/time. If the extension can be anything you probably want to use this line instead:

mv "${file}" "${file%.*}_${mydate}.${file##*.}"

Andrew

Yes I use the server date! This worked thanks! Would love to have someone show me an awk command as well!

Tbh, I see no practical reason to use awk for this. It would just open an additional process and make it more complicated than this pure shell solution provided.

What do you expect awk to do in this part? Just the name substitution? It can modify a string, but it can not rename files on it own and the date would be handed over from date into awk too.

A practical hint for the future: it pays off to be more detailed and precise when specifying a problem, and nothing should be taken for granted.
While many people interpret ddmmyy as a date format string, tt - at least for me - doesn't ring a bell, and apmcd47's proposal ignores it as well. And, if it were sth like a date format - what date to deploy? Yes, you - in a second post - said "server date", but does that make sense to use something arbitrary, unrelated to anything, the result depending on when the script was run?