Ubuntu

How to Compress Files in Ubuntu 22.04 Using Command Line?

How to Compress Files in Ubuntu 22.04 Using Command Line

The aim of Compressing a File is to keep all the information while diminishing the size of the file. Data Compression is essential to optimize data storage and data transmission as the reduced size file can be saved using less storage and can be transmitted taking lower bandwidth. There are many methods to compress files into archives in Ubuntu 22.04. Each compression method is characterized by the compression algorithm it uses to compress data.

This article will demonstrate different methods of compressing files in Ubuntu 22.04 LTS using the command line. This article will discuss:

  • Compress Files to .zip Archive
  • Compress Files to tar.gz Archive
  • Compress Files to .rar in the Archive
  • Compress Files to .7z in the Archive
  • Compress Files to Pigz in the Archive
  • Compress Files to tar.xz in the Archive
  • Compress Files to tar.bz2 in Archive

How to Compress Files to .zip Archive in Ubuntu 22.04?

“.zip” is a popular archive format as it is cross-platform compatible. “.zip” uses the “DEFLATE” compression algorithm. Files are compressed to a zip archive by launching the terminal by pressing the combination of [Ctrl+Alt+T] and by using the zip command. For example, files File1, File2, File3, File4, and File5 can be compressed to create a zipped archive “CompressedZipArchive.zip” by executing the following command:

$ zip CompressedZipArchive.zip *.txt

The above command comprises of:

  • zip: tar command line tool
  • CompressedZipArchive.zip: Name of compressed archive
  • *.txt: Select all the .txt files in the present working directory.

From the above image, it can be seen that File1, File2, File3, File4, and File5 are compressed resulting in the CompressedZipArchive.zip archive.

Further, Files can be compressed by Ubuntu UI by How to Zip Files and Directories in Linux/Ubuntu?

How to Compress Files to tar.gz Archive in Ubuntu 22.04?

The “tar.gz” is a compressed archive file format for Linux systems that uses the “gzip” compression algorithm. Files are compressed to a tar.gz archive by launching the terminal by pressing the combination of [Ctrl+Alt+T] and by using the tar command. For example, files File1, File2, File3, File4, and File5 can be compressed to create a zipped archive “CompressedTarGzArchive.zip” by executing the following command:

$ tar czf CompressedTarGzArchive.tar.gz *.txt

The above command comprises of:

  • tar: tar command line tool
  • c: Creates a new archive.
  • z: Constructs the compressed archive using “gzip”.
  • f: Write the archive – CompressedTarGzArchive.tar.gz (name of the archive file).
  • *.txt: Select all the .txt files in the present working directory.

From the above image, it can be seen that File1, File2, File3, File4, and File5 are compressed resulting in the CompressedTarGzArchive.tar.gz archive.

How to Compress Files to .rar Archive in Ubuntu 22.04?

The “.rar” (Roshal Archive) file format supports data compression, file spanning, and error correction. It is not pre-installed in Ubuntu 22.04 and can be installed by:

$ sudo apt install rar

The above image shows that rar is successfully installed. Files are compressed to a .rar archive by launching the terminal by pressing the combination of [Ctrl+Alt+T] and by using the rar command. For example, files File1, File2, File3, File4, and File5 can be compressed to create a zipped archive “CompressedRaRArchive.rar” by executing the following command:

$ rar a CompressedRaRArchive.rar *.txt

The above command comprises of:

  • rar: rar command line tool
  • a: add files to the archive
  • CompressedRaRArchive.rar: name of the archive file.
  • *.txt: Select all the .txt files in the present working directory.

From the above image, it can be seen that File1, File2, File3, File4, and File5 are compressed resulting in the CompressedRaRArchive.rar archive.

How to Compress Files to .7z Archive in Ubuntu 22.04?

7z is a cross-platform file archiver that offers the highest compression ratio. It is not pre-installed in Ubuntu and can be installed by:

$ sudo apt-get install p7zip-full

The above image shows that p7zip-full is successfully installed. Files are compressed to a .7z archive by launching the terminal by pressing the combination of [Ctrl+Alt+T] and by using the 7z command. For example, files File1, File2, File3, File4, and File5 can be compressed to create a zipped archive “Compressed7zArchive.7z” by executing the following command:

$ 7z a Compressed7zArchive.7z *.txt

The above command comprises of:

  • 7z: 7z command line tool
  • a: add files to the archive
  • Compressed7zArchive.7z: name of the archive file.
  • *.txt: Select all the .txt files in the present working directory.

From the above image, it can be seen that File1, File2, File3, File4, and File5 are compressed resulting in the Compressed7zArchive.7z archive.

How to Compress Files to Pigz Archive in Ubuntu 22.04?

Pigz is a highly efficient compression utility. Pigz is only used to compress a single file only unlike other compression utilities. Pigz is not pre-installed in Ubuntu 22.04 and can be installed by:

$ sudo apt install pigz

The above image shows that pigz is successfully installed. Files are compressed to a “pigz” archive by launching the terminal by pressing the combination of [Ctrl+Alt+T] and by using the pigz command. For example, a file File1 can be compressed to create a zipped archive “File1.txt.gz” by executing the following command:

$ pigz File1.txt

The above command comprises of:

  • pigz: pigz command line tool.
  • File1.txt: A text file that is required to be compressed.

From the above image, it can be seen that File1 is compressed resulting in File1.txt.gz archive.

How to Compress Files to tar.xz Archive in Ubuntu 22.04?

The “tar.xz” is a compressed archive file format for Linux systems that uses LZMA/LZMA2 compression algorithms. Files are compressed to a .tar.xz archive by launching the terminal by pressing the combination of [Ctrl+Alt+T] and by using the tar command. For example, files File1, File2, File3, File4, and File5 can be compressed to create a zipped archive “CompressedTarxzArchive.tar.xz” by executing the following command:

$ tar cfJ CompressedTarxzArchive.tar.xz *.txt

The above command comprises of:

  • tar: tar command line tool.
  • c: Creates a new archive.
  • f: Write the archive – CompressedTarxzArchive.tar.xz (name of the archive file).
  • J: Create the compressed files using “.xz”.
  • *.txt: Select all the .txt files in the present working directory.

From the above image, it can be seen that File1, File2, File3, File4, and File5 are compressed resulting in the “CompressedTarxzArchive.tar.xz” archive.

How to Compress Files to tar.bz2 Archive in Ubuntu 22.04?

The “.tar.bz2” is a compressed archive file format for Linux systems that is highly optimized for compression with the BZ2 compression algorithm. Files are compressed to a .tar.xz archive by launching the terminal by pressing the combination of [Ctrl+Alt+T] and by using the tar command. For example, files File1, File2, File3, File4, and File5 can be compressed to create a zipped archive “CompressedTarbz2Archive.tar.bz2” by executing the following command:

$ tar -cvjf CompressedTarbz2Archive.tar.bz2 *.txt

The above command comprises of:

  • tar: tar command line tool.
  • c: Creates a new archive.
  • v: Produce verbose output.
  • j: creates the compressed files using “bz2”.
  • f: Write to the archive – CompressedTarbz2Archive.tar.bz2: (name of the archive file)
  • *.txt: Select all the .txt files in the present working directory.

From the above image, it can be seen that File1, File2, File3, File4, and File5 are compressed resulting in the “CompressedTarbz2Archive.tar.bz2” archive.

Conclusion

There are many methods to compress files in Ubuntu 22.04. This article demonstrated seven methods of compressing files, i.e., compressing files to .zip, tar.gz, .rar, .7z, Pigz, tar.xz, and tarbz2 archives using the Linux/Ubuntu command line. The main difference between the compression methods is the compression algorithm each method uses to compress files/directories.

Similar Posts