Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
  if test -f "${file}"; then
    /bin/mv --backup=numbered "$(mktemp ${file}XXX)" "${file}"
    /bin/rm "${file}"

to be used, e.g., like this:

backup foo
curl http://.... > foo

I wonder if there is a better way.

share|improve this question
Apart from the fact that the script obviously breaks with files that contain whitespace in their path—which can be easily fixed though—why are you asking if there's anything "better"? Better in what aspect? – slhck Aug 20 '13 at 20:58
@slhck: thanks for the bug catch - should be fixed. "better" - maybe I am missing something obvious and I do not need these 7 lines? maybe there is a trivial one-liner? – sds Aug 20 '13 at 21:14

Still not exactly a one liner, but you get closer with one single call to perl together with its powerful e option (ie. execute the substitution part):

    mv "$1" "$(echo $1 |perl -pe '~s|(.*?)(~([0-9]*)~)?$|print "$1~".(${3}+1)."~"|e and exit')"

Notice the and exit what prevents perl to print the matching count that otherwise pollutes the name.

You may also want to add 2>/dev/null to the end of the line to keep it quiet when the file does not exist.

share|improve this answer
This is more complex than my solution; it does not use the --backup option of mv, it emulates it with perl. – sds Aug 21 '13 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .