Raspberry Pi

How to Compress Files on Raspberry Pi

Data compression is a technique used to compress the data size so that it consumes less space on the disk. For small devices such as Raspberry Pi, this technique is helpful since it allows you to speed up the system performance. Also, it is easy to deal with compressed data while sharing or transferring.

Follow this article to find out the ways to compress files on Raspberry Pi.

How to Compress Files on Raspberry Pi

There are multiple formats for compressed files but the most used formats for compressed files are:

Let’s disuses how you can use both format and compress the file on Raspberry Pi.

1: Compress Files Through zip

The widely used format for compressed files is the zip format and nowadays everyone knows what a zip file is. The zip file zip/compress files into a single file. To compress files using “zip” on Raspberry Pi, you must install the zip using the below-written command:

$ sudo apt install zip

 

Once the zip is installed now it can be used to zip/compress files by following the below-written syntax:

Syntax

$ zip <output file>.zip [file1] [file2] [file3] ..

 

Example

Just to quote an example, I have created 3 example files with names egfile1, egfile2 and egfile3. The below-written ls command is just to list these files:

$ ls

 

Now let’s compress them by zipping them into an opfile.zip by using the below-mentioned command:

$ zip opfile.zip egfile1 egfile2 egfile3

 

Now verify if the zip file is created successfully or not by using the same ls command:

$ ls

 

Optional: The zip can also be used to compress a directory into zip format. To compress the directory, use the directory name and a -r flag in the zip command to ensure that all files in the directory are compressed properly:

Syntax

$ zip -r <output file name>.zip \<directory name>

 

Example

$ zip -r opfile1.zip \Downloads

 

2: Compress Files through tar

The tar is the default format for compressing files on Raspberry Pi system and it’s already installed. It compresses the files or directories into an output tar file.

To compress files into a tar file, follow the below-mentioned command syntax:

Syntax

$ tar -cvjf <outputfile-name>.tar.gz [file1] [file2] [file3] ..

 

Example

$ tar -cvjf outputfile.tar.gz egfile1 egfile2 egfile3

 

To verify, I have displayed the content using the ls command:

$ ls

 

Optional: Similarly, to compress the directory, follow the below-mentioned command syntax:

Syntax

$ tar -cvjf <output-file>.tar.gz <directory>

 

The c flag is to create archive”, v is for verbose mode”, j to use bzip2” and f to allow to specify the filename of tarfile”.

Example

$ tar -cvjf opfile2.tar.gz Documents

 

Then just for verification, run ls command:

$ ls

 

Conclusion

To compress files in Raspberry Pi, two formats are most used which are: zip and tar. To use the zip format, first you must install it on the system, then you can use the zip command to compress the files on Raspberry Pi. To tar format is the default format for compressed files on Raspberry Pi and you only have to use the tar command to compress the files in the tar.gz format.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.