Skip to content
Advertisement

How to bulk add date before file extension on Linux

Lets say I have a.txt, b.txt and I want to add the date to see: a_MMDDYYY.txt, b_MMDDYYYY.txt

I’m trying to use xargs and basename in the following way:

basename -s .txt -a *.txt | xargs -n1 -i cp {}.txt {}_$date.txt

I know that the end of my command is not correct, I’m just trying to figure out how to use it and how to inject the builtin “date” into xargs.

Thanks

Advertisement

Answer

Use command substitution like this:

basename -s .txt -a *.txt | xargs -n1 -i cp {}.txt {}_$(date +%d-%m-%Y).txt

See this link for date formatting examples: https://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/

You can learn more about command substitution here: http://wiki.bash-hackers.org/syntax/expansion/cmdsubst

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement