Jill448 Jill448 - 3 years ago 120
Bash Question

Shell: Sed -i.bak to rename file by appending current date

I have sed statement to replace few contents in my file. It is taking a backup copu with the name

before replacing the content.

sed -i.bak -r "s#^(.*/abc_def_APP/).*(/application1\.war.*)#\1$version1/$version2\2#" /path/file

But I want the file to be backed up with current date. something like

How can I get it?

Answer Source

Since we have date "+%m%d%y" that returns MMDDYY (month day year):

$ date "+%m%d%y"

What about saying sed -i.$(this command) so you have the following?

sed -i.$(date "+%m%d%y") -r .... file
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download