5

I've got a tarball named test.tar which I first created it by running:

echo start > 1
tar -cf test.tar 1

And then updated it by:

echo end > 1
tar -uf test.tar 1

Now if I get a list of files within this tar archive:

$ tar -tx test.tar

It gives me:

1
1

If I try to see what's in these files:

$ tar -xOf test.tar

start
end

And finally when I want to extract it:

$ tar -xf test.tar 1
$ cat 1
end

Is there anyway to extract the old version of 1? (the one which contains start).

Another thing is deleting these files, when I use --delete parameter it'll delete all files with that name from archive. How can I only delete the old or new one?

1 Answer 1

5

From here:

--occurrence[=number]

This option can be used in conjunction with one of the subcommands >--delete, --diff, --extract or --list when a list of files is given >either on the command line or via -T option.

This option instructs tar to process only the numberth occurrence of > each named file. Number defaults to 1, so

tar -x -f archive.tar --occurrence filename

will extract the first occurrence of the member filename' from >archive.tar' and will terminate without scanning to the end of the archive.

So, in your case to extract old version use:

tar -xf test.tar --occurrence=1 1

And for deletion run:

tar --delete -f test.tar --occurrence=1 1

Another workaround to extract both version is to simply use --backup switch:

tar -xf test.tar --backup

It will extract your files like:

1
1~

Which 1~ is the old one. If you got more than of two version use --occurrence switch instead.

You can also use -w to use tar interactive mode:

tar -xf test.tar -w

This time tar asks you for each action, you can choose the first version or last version to be extract by y/n.

You must log in to answer this question.

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