Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a large file foo.tar.xz that contains a lot (say 200000) of files. I figured out that this archive contains some (around 5000) files I don't want. I don't have sufficient disk space to decompress the whole thing onto my disk; additionally, I fear attributes / rights might get lost if I do so. I have enough space to host two copies of the compressed archive though. Is there a tool to remove some of the files from the archive (specified with a regex on the filename) on-the-fly, i.e. without unpacking the archive into individual files?

share|improve this question

(edited, as I misunderstood the question, which was since edited also)

The best you can do is to extract, delete, and recompress the entire file.

unxz < foobar-old.tar.xz | tar --delete foo/bar | xz > foobar-new.tar.xz

It's not possible to delete files from a tar directly.

tar is a stream, originally intended for tape drives which do not do random seeks well - while in theory it could be possible on a disk filesystem to punch a hole / rewrite the remaining file, with compression the point is moot as most if not all compression methods heavily depend on contents that occured earlier in the file. In order to do this in place you would need very detailed knowledge about both the compression method as well as the tar file format. That's complexity to a point no one would even bother with it. It's cheaper to just keep the files around and ignore them.

If you need this functionality, tar is probably not what you want.

share|improve this answer
Those files make up 35% of the archives size. The restrictions you point out seemingly do only apply if I rewrite the file, not if I modify it out-of-place, which I can do (I have enough space to save the packed archive twice). Is there such a tool? – FUZxxl Mar 21 '13 at 20:57
I may have misunderstood your question then. If you ARE willing to unpack the tar after all, and repack it, (just without actually creating the tarred files - i.e., a direct tar to tar pipe), it may be possible. – frostschutz Mar 21 '13 at 21:01
Yeah, I can do that. It's just that the files have uids/gids/attributes that I need to preserve. Also, I do not have enough disk space to save the unpacked representation. I have enough space to save two packed archives though. – FUZxxl Mar 21 '13 at 21:05
unxz < foobar-old.tar.xz | tar --delete foo/bar | xz > foobar-new.tar.xz – frostschutz Mar 21 '13 at 21:11
Does this scale? I have 5000 files which I can describe using the regex data/.*\.bak AFAIK delete does only recognize individual files. – FUZxxl Mar 21 '13 at 21:13

According to the manual, you can pass a list of filenames to tar to only extract those. For example:

$ tar --file archive.tar --list

$ tar --file archive.tar --extract foo
share|improve this answer
I don't see how --extract helps me. Could you elaborate? Please keep in mind that I cannot unpack the archive (or substantial parts of it) to disk. – FUZxxl Mar 21 '13 at 21:07
Please do not just post links: this is a wiki--add sufficient content for it to be unnecessary for people to leave the page to understand your answer. – jasonwryan Mar 21 '13 at 22:02

Your Answer


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.