Home Linux BasicsHow to Compress and Extract Files in Linux: tar, gzip and zip

How to Compress and Extract Files in Linux: tar, gzip and zip

by Knowledge Base Linux
How to Compress and Extract Files — How to Compress and Extract Files in Linux: tar, gzip and zip

Sooner or later every Linux user needs to bundle a whole folder into one file, shrink it so it is easier to send, and later unpack it again. Learning how to compress files in Linux with tar, gzip and zip is one of those everyday skills you will use for backups, sharing projects and downloading software. The good news: the tools are already installed, and the basic commands are easy once someone explains what each letter means. This article covers How to Compress and Extract Files in detail.

Two Jobs, Two Tools: Bundling and Squeezing – How to Compress and Extract Files

Beginners often wonder why Linux needs both tar and gzip. The answer is that they do different jobs:

  • tar bundles many files and folders into a single file, like putting books into one box. It does not make anything smaller. The result is called a tarball and ends in .tar.
  • gzip squeezes a single file to make it smaller, like vacuum-packing the box. The result ends in .gz.

Used together, you get the famous .tar.gz file (sometimes shortened to .tgz): first bundled by tar, then squeezed by gzip. When people say “compress files in Linux”, this combination is what they usually mean. If you want to squeeze one single file only, our dedicated guide to the gzip command in Linux covers it in depth.

Compress Files in Linux with tar and gzip – How to Compress and Extract Files

To bundle a folder named myproject and compress it in one step, run:

tar -czvf myproject.tar.gz myproject/

Those four letters look scary, but each one has a simple meaning:

  • c – create a new archive
  • z – compress with gzip
  • v – verbose: show me each file as it is added
  • f – the next thing I type is the archive file name

A friendly way to remember the create command is “Compress Zee Files!” To make a plain bundle with no compression, simply drop the z: tar -cvf myproject.tar myproject/. You can also add several items at once: tar -czvf backup.tar.gz Documents/ Pictures/.

Compress files in Linux: opening an archive and unpacking folders with unzip

Extract Files in Linux: Unpacking a tar.gz Archive

Extracting is the mirror image. Change the c to an x (for extract):

tar -xzvf myproject.tar.gz

The files appear in your current folder. Beginners often unpack archives in the wrong place and scatter files everywhere, so a safer habit is to extract into a chosen folder with the -C option:

mkdir unpacked
tar -xzvf myproject.tar.gz -C unpacked/

Curious what is inside an archive before opening it? List its contents with t: tar -tzvf myproject.tar.gz. This quick peek can save you from messy surprises.

Compress Files in Linux with zip and unzip

The zip format is the same one Windows and macOS use, which makes it perfect for sharing files with people on other systems. To compress files in Linux using zip:

zip photos.zip photo1.jpg photo2.jpg
zip -r website.zip website/

The -r means “recursive”: include the folder and everything inside it. Without -r, zip would only store the empty folder itself, which confuses many first-timers. Extracting is just as friendly:

unzip photos.zip
unzip website.zip -d restored/

The -d option chooses the destination folder. Note that on a fresh server you may need to install these two small tools first (for example sudo apt install zip unzip on Debian and Ubuntu), while tar and gzip are always present.

A Quick Word About .tar.bz2 and .tar.xz

While downloading software you will also meet two cousins of the tarball. Files ending in .tar.bz2 use bzip2 compression (flag j instead of z), and files ending in .tar.xz use xz compression (flag J). They squeeze a little tighter than gzip but work a little slower. The comforting part: the workflow is identical. To extract either kind, most beginners can simply run tar -xvf filename and let tar detect the format by itself — modern tar is smart that way.

Common Beginner Mistakes

  • Forgetting the -f flag. Without f, tar tries to use an old tape device (yes, really) and seems to hang. The file name must come right after the flags.
  • Wrong flag order. In tar -czvf archive.tar.gz folder/, the archive name must immediately follow f. Putting the folder first creates a very confusing error.
  • Extracting into a crowded folder. Always check the contents with tar -tzvf first, or extract with -C into a fresh folder.
  • Expecting tar alone to shrink files. A plain .tar is the same size as the originals. You need the z (gzip) or J (xz) flag for real compression.
  • Zipping a folder without -r. The archive looks fine but contains an empty folder.

Conclusion

You now know the everyday recipe to compress files in Linux: use tar -czvf to bundle and squeeze, tar -xzvf to unpack, tar -tzvf to peek inside, and zip -r / unzip when you need the friendlier cross-platform format. These few commands cover backups, downloads and sharing. Compressed backups are also the first step of a good safety routine; tools like the one in our Ubuntu backup guide with Restic build on the same idea. For every flag tar offers, the official GNU tar manual is the complete reference.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.