How to extract tar.gz, tar.xz and zip files, pack a folder, look inside an archive without extracting it, and leave out files you don’t need. What to do if you’ve downloaded a program as a tar.gz.
tar gathers files and folders into a single file, and a separate program compresses it — hence the double extensions: .tar.gz (gzip), .tar.xz (xz), .tar.zst (zstd), .tar.bz2 (bzip2). You can extract all of them with one command: tar works out for itself how the archive was compressed.
Extracting
tar -xf archive.tar.gz
tar -xf archive.tar.gz -C /var/www/site # into another folder — it must already existx means extract, and f means the archive file, whose name goes straight after f. Add v to see the list of files: tar -xvf. Older guides write tar -xzf — that works too; the z just isn’t required when extracting.
To extract a single file: tar -xf archive.tar.gz folder/file.txt — use the path as it appears inside the archive.
If you extract with sudo, the files keep the same owners and permissions as in the archive. Without sudo, all the files will belong to you.
Looking inside
tar -tf archive.tar.gz | lessThis is useful before extracting: a tidy archive puts everything in one folder, while a messy one scatters hundreds of files straight into the current directory.
Packing
tar -czf site.tar.gz -C /var/www example.co.ukc means create an archive, and z means compress with gzip. -C /var/www changes to that folder before packing, so the paths inside the archive start with example.co.uk/… rather than var/www/example.co.uk/….
| Option | Compression | Extension |
|---|---|---|
| -z | gzip — fast, understood by everything | .tar.gz, .tgz |
| --zstd | zstd — faster than gzip and compresses better | .tar.zst |
| -J | xz — the strongest compression, but slow | .tar.xz |
| -j | bzip2 — found in older archives | .tar.bz2 |
To leave out what you don’t need, such as cache and logs:
tar -czf site.tar.gz --exclude='example.co.uk/cache' --exclude='*.log' -C /var/www example.co.ukzip
The zip programs are usually not installed on cloud images:
sudo apt install zip unzip # Ubuntu, Debian
sudo dnf install zip unzip # AlmaLinuxunzip -l archive.zip # what is inside
unzip archive.zip -d folder # extract into a folder
zip -r site.zip example.co.uk # pack a folderIf an archive made on Windows shows garbled file names with non-Latin characters, use the encoding option of unzip on Ubuntu and Debian to specify the encoding it was created with — for example, unzip -O cp866 archive.zip for Cyrillic names.
I downloaded a program as a tar.gz — how do I install it?
First look for it in the system’s packages: apt search name or dnf search name. A program installed from packages is updated along with the system, while one downloaded as an archive you will have to update yourself.
If there is no package, extract the archive and look inside. There is usually a README or INSTALL file with instructions. After that there are two possibilities:
- A ready-made program — a single executable file. Put it in
/usr/local/binand make it executable:sudo install -m 755 program /usr/local/bin/. - Source code — there is a
configurefile or aMakefile. You build it with./configure,makeandsudo make install. Building needs a compiler:sudo apt install build-essentialorsudo dnf groupinstall "Development Tools".
Didn’t find the answer? Ask our support team — we’ll help.