Re: Extracting indiviual files or directories from XYZ.tar.xz - Possible?
On Thu, Aug 7, 2025 at 5:01 AM Richard Owlett <rowlett@access.net> wrote:
> My questions:
> 1. Can individual files or directories be extracted from XYZ.tar.xz ?
Yes.
$ cd $(mktemp -d)
$ >f
$ mkdir d
$ >d/f
$ tar -cf - [fd] | xz -9 > tar.xz
$ rm -rf [fd]
$ ls -A
tar.xz
$
So, we now have our tar.xz file.
Can use tar's -t option for Table of contents,
optionally with -v for Verbose, and those can be bundled.
Many tar versions even allow, first argument, which needs be option(s)
anyway, to not even include the leading - character, so, e.g.:
< tar.xz | xz -d | tar tf -
$ < tar.xz xz -d | tar tf -
d/
d/f
f
And the f option says our corresponding following option argument
specifies the file to use for the archive, and if - is specified,
that means read the archive from stdin, or write it to stdout.
Now, if we want to extract other than all the contents, we just give
additional arguments, when using -x (to eXtract), rather than t.
However when tar is given directory(/ies) to extract, it does so
recursively, and some (e.g. GNU) versions of tar have options to
allow that to be non-recursive, but some don't.
Debian offers multiple versions of tar, e.g GNU tar, busybox tar,
bsdtar, ptar, ...
In such a case, can use version of tar that has such an option,
or we could use pax.
pax can deal with several tar and cpio formats.
So ...
No options to pax, by default it reads stdin,
and list contents, like tar's [-]t option.
$ < tar.xz xz -d | pax
d
d/f
f
$
With non-option arguments, it will match to those,
using shell globbing like syntax, with one highly notable
exception, namely * matches any character, including the / character.
And, note also, since * is special to shell, would generally want to
quote that.
On directories, by default we have recursion,
but if we don't want that, we add the -d option, e.g.:
$ < tar.xz xz -d | pax d
d
d/f
$ < tar.xz xz -d | pax -d d
d
$
And to extract, we use the -r option (Read, as it's reading from
an archive, -w for Write to create an archive).
We also have -p option and its arguments, for permissions,
e.g. -p e for "Everything" (including ownerships), or
-p p for "Permissions"
$ ls -A
tar.xz
$ < tar.xz xz -d | pax -rd -p p d
$ ls -A
d tar.xz
$ find d -print
d
$ ls -nd --full-time d; date --iso-8601=seconds
drwx------ 2 1003 100 40 2025-08-09 07:34:12.000000000 +0000 d
2025-08-09T08:11:51+00:00
$
And though some tar and other archive programs offer (de)compression,
by keeping that separate, we can use any (de)compression programs
we want, including one(s) our archive programs don't know how to handle.
Reply to: