Bill Lovett

Saving Old File Versions with Bash

Posted on April 9th, 2006

Sometimes you want to back a backup copy of a file prior to overwriting it with a newer version. So you reach for the mv command, like so:

$ mv example.txt example.txt.bak

Before long you've got example.txt.bak, example.txt.bak2, maybe even example.old or some other variation. Messy. A simple bash function placed in your .bashrc file can do the job better:

function bk() {
    mv $1 $1-$(date +%F-%I-%M).bak
}

Where you would have used mv before, now use bk. It will move the file but also tack on a timestamp. Consistent!

Back to the index of all blog entries