How to Compress Files and Directories in Linux

It is pretty common to compress files or directories to send them via email, or just to simply save space on our systems. This guide will show you how to compress files and directories in Linux.

How to Compress Files and Directories in Linux

TAR

The tar command allows you easily group files and directories into an archive. Also it can extract .tar files, list all the contents of the archive, add extra files to the archive, among many other things.

It is important to note that the tar command allows you to use many other compressions algorithms such as: bzip2, gzip, and many others.

Syntax

tar <options> <archivename.tar> files or directories

Some of the most common options are:

  • -c: Create a new archive. (--create)
  • -v: Verbose mode. (--verbose)
  • -f: Allows you to specify the archive name. Short for --file=archive=archive_name
  • and many more

Whether you are wanting to compress files or directories, both can be listed separated by a space.

.tar

To compress files and directories in Linux using the tar command with the .tar extension:

tar <options> <archivename.tar> files or directories
example files

tar -cvf my_photos.tar photo1.jpg photo2.jpg photo3.jpg

same as

tar --create --verbose --file=my_photos.tar photo1.jpg photo2.jpg photo3.jpg
example directory

tar -cvf my_photos.tar photos

or

tar -cvf my_photos.tar photos wedding vacation
.tar.gz

As mentioned before we can use the .tar command to compress and directories in Linux. In this case we’ll use one of the most popular algorithms (gzip) to compress files and directories in Linux.

The syntax is similar to the .tar syntax, but in this case we need to tell tar to use the gzip algorithm with the help of the -z flag:

tar -czf archive.tar.gz file/directory1 file/directory2
Example files

┌─[linuxify@linuxify]─[~/Desktop]
└──╼ $tar -czf archive.tar.gz demo.txt demo2.txt demo3.txt 
┌─[linuxify@linuxify]─[~/Desktop]
└──╼ $
.tar.gz entire directory
.tar.bz2

bzip2 is another popular compression algorithm that uses the Burrows–Wheeler algorithm to compress files. As this algorithm only compresses files and not archives, with the help of tar we can compress and create archives.

Similar to .tar.gz, in order to compress files and directories in linux with the bzip2 algorithm we need to use the -j flag when using the tar command:

tar -cjf archive.tar.gz file/directory1 file/directory2

ZIP

Zip is one of the most popular archive file format that is currently supported by default on many operating systems including Linux, macOS, and Windows.

Syntax

In order for us to compress files and directories in Linux with the zip utility we need to understand the syntax of it:

zip options file_name.zip files_to_compress

Here’s a list of the most commonly used zip options:

  • -r: Recursive mode.
  • -v: Verbose mode.
  • -Z: Specify the compression method.
  • -q: Polar opposite to verbose mode. Quiet mode, no outputs.
  • -e: Create a password encrypted archive.
  • -0-to-9: Compression level, just use one.

Do note that by default the compression level of zip by default is set to deflate. If the file cannot be compressed, zip will simply store the file in the archive with no compression.

Compression Level

The zip utility allows us to specify the compression level. The higher the compression the more resources and time it will take.

zip -9 filename.zip files_to_compress
Recursive

In order for zip to recursively zip an entire directory, we need to specify the -r flag:/

zip -r
zip with bzip2

Similar to tar, zip can also be used along with the bzip2 algorithm. This can be done by using the -z flag to specify the compression method (--compression-method)

zip -r -Z bzip2 archive.zip files_to_compress
password-protected

To create password-protected archives, we can specify the password by using the -e flag:

zip -e files.zip files
zip password protected - How to compress files and directories in linux

Summary

This guide showed you how to easily compress files and directories in Linux.

Leave a Comment