views:

374

answers:

5

This question notes that it is possible to overwrite files when creating a tar archive, and I'm trying to see how to avoid that situation.

Normally, I'd use file roller, but the version installed is playing up a bit (using 1.1 Gb of memory), and I'm not the system administrator.

I looked at --confirmation and --interactive, but that only asks me if I want to add file x to the archive, not whether I want to overwrite an existing file. For example,

tar --interactive -czvf innocent_text_file.txt foo*

Will ask me about each file, but is perfectly happy to overwrite innocent_text_file.txt

Is there any switch that acts like -i for cp?

Note I am asking about creating an archive, not extracting an archive.

Clarification What I'm worried about is accidentally doing something like this

tar -czvf * #Don't do this!

which would overwrite the first file listed in the glob. To avoid it, I want tar to complain if the first file mentioned already exists, like

cp -i * #Don't do this!

would check if it would cause you to overwrite an existing file.

A: 

If I understood your question correctly, this should do the trick

-k, --keep-old-files keep existing files; don’t overwrite them from archive

hhafez
Doesn't seem to work. innocent_text_file.txt gets overwritten when the erroneous tar command for archive *creation* is given.
Andrew Grimm
A: 

What about "u" (update) instead of "c" (create)? e.g., tar -uzvf

That should only add (and overwrite) the file if it's newer than the one in the archive.

Tim Sylvester
Sorry, this doesn't help either.
Andrew Grimm
A: 

These are overwrite controls while extracting from a tar.

Overwrite control:

-k, --keep-old-files  don't replace existing files when extracting
  --keep-newer-files  don't replace existing files that are newer than
                      their archive copies
  --no-overwrite-dir  preserve metadata of existing directories
  --overwrite         overwrite existing files when extracting
  --overwrite-dir     overwrite metadata of existing directories when
                      extracting (default)
  --recursive-unlink  empty hierarchies prior to extracting directory
  --remove-files      remove files after adding them to the archive

But, you refer to cp -i and tar c operations for reference.
Which implies you are interested in updating an archive that does not overwrite an existing copy in the tar with a new version. I suspect that control is not directly available with tar.
I also wonder at the need for such a scheme.

However, one way to go about this would be as follows.
Since, you are worried about overwrites into the archive,
we safely assume you have an existing partial tar of the directory (say files.tar).

cd base/dir/to/be/tarred 
tar tf path/to/files.tar | sort > archived-files.lst
find . -type f | sort > files.lst
diff archived-files.lst files.lst | grep "^>" | cut -c 3- > new.lst
tar uf -T new.lst

Note that this update works only on uncompressed archives.
So, if your archive is compressed gunzip/bunzip2 it as appropriate before this sequence.
and, then re-compress it at the end.

nik
I'm asking about archive creation, not extraction.
Andrew Grimm
Yes, I figured, but it sounds like an unusual requirement. You really want to retain the first copy of the file in the archive even if its is modified/updated in the live directory?
nik
I've added a clarification at the end of the original question. Does it make more sense now?
Andrew Grimm
About `clobbering` existing files by mistakenly missing out the target archive name on the command line, I recollect `tar` used to avoid doing that when the `c` option for create was used. However, a quick check on my local Cygwin did no do that. I am surprised.
nik
As an aside, the Unix command prompt is a holy place. More so when you are root/admin. It does not forgive and helps you forget. I remember the resident roots at our university used to categorically avoid the root shell on drunk late evenings!
nik
+1  A: 

If I understand your question correctly I believe this should to the trick:

tar -cv * | gzip > myfile.tgz

HopefullyHelpful
+1  A: 

I've been looking for something somewhat similar. Why isn't there a universal noclobber option? Sure, noclobber works if you try to do anycommand > anyfile, where anyfile already exists, but how about a noclobber that prevents things like tar cf tarfile filelist from overwriting tarfile if it already exists, or prevents sftp from overwriting files on the destination disk if they already exist?

In other words, a noclobber that works for all commands?

Bob Stanton