Skip to content
RIPE NCC member · Local Internet RegistryCall us on 020 3318 3449sales@hosting-telecom.uk

Archives: tar, gz and zip

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.

Contents

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 exist

x 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 | less

This 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.uk

c 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/….

OptionCompressionExtension
-zgzip — fast, understood by everything.tar.gz, .tgz
--zstdzstd — faster than gzip and compresses better.tar.zst
-Jxz — the strongest compression, but slow.tar.xz
-jbzip2 — 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.uk

zip

The zip programs are usually not installed on cloud images:

sudo apt install zip unzip    # Ubuntu, Debian
sudo dnf install zip unzip    # AlmaLinux
unzip -l archive.zip              # what is inside
unzip archive.zip -d folder        # extract into a folder
zip -r site.zip example.co.uk      # pack a folder

If 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/bin and make it executable: sudo install -m 755 program /usr/local/bin/.
  • Source code — there is a configure file or a Makefile. You build it with ./configure, make and sudo make install. Building needs a compiler: sudo apt install build-essential or sudo dnf groupinstall "Development Tools".

Didn’t find the answer? Ask our support team — we’ll help.

Choose which cookies we may use. You can change your mind at any time with the “Cookie settings” link at the bottom of every page.

Necessary

Always active

They keep the site secure and remember your cookie choice. They don’t identify you.

Google Analytics counts visits and shows which pages people find useful, so we can improve the site. Which cookies