Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

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
1  
@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

1 Answer 1

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):

backup(){
    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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.