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.

Pretty noob move but used tar with the source and destination file arguments swapped resulting in a complete mess (the source file has been overwritten with another file of same name with size 4kb).

Is there any way to recover my original file?

As an aside, I'm surprised the default is to overwrite but I suppose I shouldn't be doing something like this before my morning coffee. Ugh.

share|improve this question
add comment

2 Answers

Unless you have a backup of the original source file stored elsewhere, you are unfortunately out of luck. Tar has no way of recovering from this particular error condition.

share|improve this answer
add comment

Maybe this could be an inspiring moment to write a script. Rename the tar command as tar.orig, and name your new script my-tar.sh. Have your new my-tar.sh script:

  1. perform a sanity check as to which is input and which is output (check .ext? filesize? date-time stamp?), then
  2. create a backup of your input file (cp input.bkp) before any processing is done.

You could also include a small piece of menu, configured to easily select between your favorite tar command option sets!

If you're feeling truly robust, you could also have your script rotate several backup files of each of your most recent 'runs' (using /tmp dir; .bkp, .bk4, .bk3, .bk2, .bk1), just in case you manage a good bit of keyboard-madness before that first cup-o-joe! Maybe also rotate a small log file as /var/log/my-tar.log to track date & time stamps and exact script-processed command line of what was executed during each of the last several 'runs' of your customized script!

For help with writing the mytar.sh script, bite the bullet and go the educational route: grab yourself an online PDF of "Advanced Bash-Scripting Guide" from The Linux Documentation Project. You'll be worlds ahead of the rest by spending the time to learn how to handle many script-needy situations - before they bite you from behind!

share|improve this answer
add comment

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.