tar.gz file into the current directory:
tar -xvf /path/to/archive
Zip/tar a directory into a compressed archive:
tar -cavf /path/to/archive.tar.gz /path/to/folder
If you're ever on Linux,
tar might be one of those commands that you copy/paste every single time. Whether you're following the guide to install Golang and you paste in the command that's listed in the guide, or you encounter a stray
tar.gz file and now you gotta look up the plethora of flags to figure out what you need.
Let's demystify tar. You're welcome to use this article as a guide or just as a cheatsheet. I'm writing it as both...for myself.
tarball is a single file that represents many files. You can call it an
archive but before compression, it really was just a way to represent a file directory and collection of files as a single file.
You might be more familiar with a
zip file (which is an archive but with compression) or a
rar which is another compressed archive format.
On top of allowing us to transport entire directories as "one file", tar also allowed fun stuff like
checksum, providing a file size for the entire archive, and later, compression.
tar.gz is a compressed
tar archive. If you ever see just
.tar on an archive, it means that there's no compression. With
tar.zst), you always expect compression. The
gz itself might seem familiar and that's because the
tar archive was compressed via
Gzip is a compression tool but it is not an archive tool. So you can
gzip a single file (web servers will
gzip individual files before sending them over the wire) but not a collection of files. Since a
tar is a collection of files put together to create a single file, gzip can compress that.
The tar command breaks down to:
tar [options] file/path
It feels like there are an endless amount of options and I think that goes with any
unix tool. It's straightforward but so customizable.
The flags you need to remember are:
vfor verbose output (recommended)
flets you specify the file you want to extract
tar is unixy and can process data that's been piped over.
.tar.gz file, the entire command is:
tar -xvf path/to/archive
This will extract current file into the current directory.
The tar command is very similar for compressing/archiving files but the command looks a bit different:
tar [options] /path/to/destination/archive.tar.gz path/to/file path/maybe/to/folder
tar adds every path you specify into the resulting archive so you don't have to
tar a single file or have to pre-group all files in a folder.
It also requires different flags:
tarto create an archive
tarto compress but only when the file extension calls tar to do it (explanation below)
flets you pick the name of the archive to save into
For compression to work, you need to specify the extension on the archive matching the compression tool. So if you want your archive to be gzipped, make sure to add
.gz to your archive name.
The entire command is:
tar -cavf /path/to/archive.tar.gz /paths/to/files/or/folders
You might have already noticed but the
tar command simplifies a lot more or there's a lot less to remember if you keep this in mind:
vfflags on all standalone commands
ameans auto-compress (used only with