3

The tar command just overwrote my file an hour ago while I was trying to make a tar to backup it.

I ran

tar zcvf foo.tex foo.bib

and after that my tex source file lost. What's worse, this is the second time I lost my file in this way.

I use gvim, but 'u' command can recover my file. After browsing some help entries about 'backup', 'undofile', I just can not find a way to avoid tar command from overwriting my file again.

Then I try to find a parameter like '-i' from cp or mv, but among the many parameters of tar I can't find one.

I wonder how can I avoid this kind of things happening again, for I am a little unconfident about remembering to add the new foo.tar thing that I always forget next time.

2

Add this to your .bash_login file. alias tar=tar --backup=simple This will append a ~ to the file it is about to overwrite.

So when you create the archive it will always run

tar --backup=simple zcvf foo.tex foo.bib

and after execution a foo.tex~ will exist (this is the actual tex file and the archive will be in at foo.tex)

  • Thanks,this is exactly the answer that I need.(Why didn't I discover the --backup option when searching the man page...). Another question :what does the CONTROL in the man page mean,what are some other CONTROL options except 'simple' in your answer? – bigeast Jun 5 '13 at 9:44
  • @bigeast instead of simple, the alternative is number iirc. – sparticvs Jun 5 '13 at 12:31
  • @sparticvs I tried "tar --backup=simple" in my Terminal (bash) and got the error "tar: Option --backup=simple is not supported". What is "number iirc"? (Noob level here. Just started learning using shells). – politicus Dec 6 '17 at 15:36
  • @bigeast I couldn't find it in the man pages either. – politicus Dec 6 '17 at 15:39
  • 1
    @politicus iirc = if i remember correctly. Another alternative to tar --backup=simple is tar --backup=numbered which will number the backups sequentially. – sparticvs Jan 24 '18 at 20:41
1

Use output redirection instead of specifying a file name. This applies to many commands, not just tar.

Of course you have set -o noclobber (also spelled set -C) in your .bashrc, right?

$ tar zc >foo.tex foo.bib
bash: foo.tex: cannot overwrite existing file

If you often issue this command, don't type it manually: write a makefile.

foo.tar.gz: foo.tex foo.bib
        tar czf $@ $^

Use tarballs only when you need to send them to someone else, not to make backups. Instead of making backups of old versions, use version control (CVS, Mercurial, Git, etc., whatever floats your boat). (Do back up the repository in case your disk fails or you accidentally mangle or erase the repository.)

  • I haven't heard of 'noclobber' option before,but now I find it's useful.And thank you for your other advices. – bigeast Jun 5 '13 at 9:54
1

This problem occurs because 'short' options for tar allow you to combine things in a way that obfuscates that the foo.tex belongs to the f option.

The full version of your command (with a modern version of tar) would be:

tar --create --file foo.tex --gzip --verbose foo.big

or with shortened options:

tar -c -f foo.tex -z -v foo.big

Both these are, in my experience, way more obviously incorrectly writing to a file foo.tex.

If you would do:

tar --create --file --gzip --verbose foo.tex foo.big

You get a file --gzip, and this is something I hope you do not regularly use as a filename, so that overwriting is not a problem. (BTW you can remove a file with such a filename with rm -- --gzip.)

The one character options for tar without - are an older/original way to specify options. Unix dinosaurs, like me, tend to use them, but I think they are confusing. I recommend using the fuller 'dashed' versions seperately until you feel more confident using tar.

0

You might prefer the behaviour of 7z (7-Zip). I couldn't get it to overwrite an existing file when compressing. It supports tar output (-t tar).

There's also atool - I tried it and the UI looks nice:

apack a b
apack: a: format not known, identifying using file
apack: a: format is `tar'
apack: a: refusing to overwrite existing file
0

First it is quite easy to remember the right characters:

  • czf = create zipped file
  • xzf = extract zipped file

If you think this way you should always get it right.

The other thing with important data is, having it in some version control. I recommend git for that. It will save you all your history locally and allows you to push all of it to any other medium. - Very handy and no more backups using tar.

0

I am not sure why you are giving the same file name as your source to the tar ball.

if the source file is foo.tex, that needs to be archived then you should do the following:

tar zcvf foo.tgz foo.tex

if there are mulitple files then do the following

 tar zcvf foo.tgz foo.tex foo.bib 

if you are trying to do the incremental tar ball then use this link. I have not tried it yet but I think it is best to use git if you are trying to do version control.

  • That's why I my tex source file is overwritten by the gziped file of the later bib file. – bigeast Jun 5 '13 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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