Almost all downloadable files on the internet are compressed. The reduced file size makes data transfer quicker and also cheaper. That is why in this tutorial, we’re going to learn how to compress and extract compressed files on a Raspberry Pi.

File Compression

File compression is putting one or more files in an archive so that their cumulative size is reduced. Having a smaller file is advantageous when dealing with the internet. Firstly, they won’t take too much space. Hosting a website, especially for DIY projects using an Arduino or a Raspberry Pi, is often limited in memory space. With a smaller file size, data storage is relieved. Also, for rated internet services, a reduced file size means saving money.

There are more than a dozen compressed file formats – zip, tar, rar, 7z, arc, arj, cab, and many more. But the most common are zip and tar. So in this article we will just focus on how to compress and extract zip and tar files.

Zip Files

One of the earliest compressed file formats is the zip format. Zipped files are very popular and most people today know what a zipped file is.

Extracting Zip Files

Extracting a file means getting the original files out of a compressed file format. To uncompress a zip file, simply write the command below.

unzip filename.zip

The zip and unzip commands are default to the Raspberry Pi OS, so no need to install them explicitly. The command is also straightforward. Just enter unzip then the file name of the archive file. The compressed files inside will go to your current directory in no particular order.

Compressing Zip Files

On the other hand, if you want to compress one or multiple files into a zip archive, use the command syntax below.

zip filename.zip file1 file2 file3

Take note that files 1 to 3 must be located in your current directory. Additionally, if you want to compress a whole directory to a zip file, enter the following command.

zip -r filename.zip /targetdirectory

The -r switch makes sure that all the files in your target directory are compressed properly.

Tar Files

Meanwhile, the default compressed file of the Raspberry Pi is called a tar file. They are the output of a GNU Linux command called tar. Tar in itself bundles files together without compressing. These bundled files are called a “tarball.” Tar is often paired with gzip to compress files into a .tar.gz file or .tar.bz2 file.

Tar can be accessed directly using the terminal. This is why it’s the preferred compressed file type on a Linux environment.

Extracting Tar Files

To get the original files from a tar archive, enter the command below.

tar -xzvf filename.tar.gz

The tar utility has a similar syntax with Linux-based commands. It uses a couple of switches that may seem daunting at first but are actually very convenient. Here’s what the switches on the sample command mean:

-rExtract an archive.
-zUse gzip.
-vEnter verbose mode. Verbose means it will display all that’s happening to your files.
-fLets you specify the filename of tar file.

Compressing Tar Files

Similarly, to compress using tar, we use the command below.

tar -cvjf filename.tar.gz /targetdirectory

The switches are almost the same as the ones we used when extracting except -c and -j, which means creating an archive and using bzip2, respectively.

-cCreate an archive.
-jUse bzip2.

Hope this article has helped you to better understand how to compress and extract files on the Raspberry Pi. Please leave a comment below if you have questions about anything!