How to Extract Specific Files from .tar.gz
files #
It recently came to my attention that a lot of software on Github is being distributed through their releases interface, specifically in .tar.gz
archive files. This is a perfectly fine format, but tar
has a reputation for being a rather obtuse program to remember.
As a result, I found myself wondering: Can I download a tar.gz
file, pipe it into tar
and only extract the one file I want, thus installing the uv
binary by simply dropping it into /usr/local/bin
. And the answer is yes! Apparently I can! It just took some looking through documentation and searching through Stack Overflow.
Here's the basic example:
1wget -c https://github.com/astral-sh/uv/releases/download/0.5.5/uv-x86_64-unknown-linux-gnu.tar.gz -O - | tar -zxv --strip-components 1 -C /usr/local/bin uv-x86_64-unknown-linux-gnu/uv;
So what's going on here?
wget
is a pretty well known command, I won't spend much time on it.- The important part is
-O -
which tellswget
to write the output to the pipe (which is what the-
represents).
- The important part is
tar
however, is the main focus of today's presentation.-z
: filter archive throughgzip
, use to decompress.gz
files.-x
: instructstar
to extract files.-v
: Verbose (show progress while extracting files).--strip-components 1
: instructstar
to strip away one "layer" of directories from inside the archive. How do we know that the archive has directories inside, what what are they? We can use this command:tar -ztf <ARCHIVE>
to list the contents of the archive (<ARCHIVE>
can be omitted if its being piped in). If we execute the command on theuv
archive, we would see that the file we care about is:uv-x86_64-unknown-linux-gnu/uv
so one directory will need to be stripped away.-C /usr/local/bin
: tells us to move to the/usr/local/bin
directory before extracting, which is how we specify where the file should be extracted.uv-x86_64-unknown-linux-gnu/uv
: The file being extracted from the archive.
The end result of this is that /usr/local/bin/uv
will be present on your system (and usable).
References:
- https://stackoverflow.com/questions/14295771/how-do-i-extract-files-without-folder-structure-using-tar - describes how
--strip-components
works. - https://www.tecmint.com/download-and-extract-tar-files-with-one-command/ - describes how to pipe stuff from
wget
intotar
. - https://unix.stackexchange.com/questions/61461/how-to-extract-specific-files-from-tar-gz - describes how to specify a file to extract from
tar
.