Jill448 Jill448 - 2 years ago 100
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

file.bak
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
file.071913


How can I get it?

Answer Source

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

$ date "+%m%d%y"
071913

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