Tuesday, 20 August 2013

How do I create numbered backups?

How do I create numbered backups?

Some GNU utils (e.g., mv and cp) can create numbered backups (foo.~1~).
Others (e.g., wget) cannot. I would like to create numbered backups before
running tools which overwrite files by default. Here is a bash function
which appears to do what I need:
backup(){ # back up the file, emacs style
file=$1
if test -f "${file}"; then
/bin/mv --backup=numbered "$(mktemp ${file}XXX)" "${file}"
/bin/rm "${file}"
fi
}
to be used, e.g., like this:
backup foo
curl http://.... > foo
I wonder if there is a better way.

No comments:

Post a Comment