
How to Compress Files in Linux/Ubuntu Terminal?
Digital data is increasing by multiple folds. The data is typically organized in Files. Large files are usually compressed to conserve storage space and transmission bandwidth. The main aim of data compression is to minimize the file size while retaining all the information. There are several compressed archive file formats to compress files into archives in Linux/Ubuntu systems. All these compressed archive file formats are characterized by the compression algorithm they use to compress data, resulting in different compression efficiency.
This article will demonstrate the below-listed ways of compressing files into Archives via Linux/Ubuntu Terminal:
- How to Compress Files to tar Archive in Linux/Ubuntu Terminal?
- How to Compress Files to tar.gz Archive in Linux/Ubuntu Terminal?
- How to Compress Files to tar.xz Archive in Linux/Ubuntu Terminal?
- How to Compress Files to tar.bz2 Archive in Linux/Ubuntu Terminal?
- How to Compress Files to .zip Archive in Linux/Ubuntu Terminal?
- How to Compress Files to .7z Archive in Linux/Ubuntu Terminal?
- How to Compress Files to .rar Archive in Linux/Ubuntu Terminal?
- How to Compress Files to Pigz Archive in Linux/Ubuntu Terminal?
- How to Compress Files to .gzip Archive in Linux/Ubuntu Terminal?
How to Compress Files to tar Archive in Linux/Ubuntu Terminal?
The tar command is usually used to bundle files in a single archive. The “tar” command additionally provides a compression feature. Files are compressed to a tar archive by using the tar command. For example, to compress the files: File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt to tar archive “TarArchive.tar”, execute the following command:
$ tar cfv TarArchive.tar *.txt
Where:
- c: Creates a new tar archive.
- f: Write to the archive
- v: Produce verbose output.
- *.txt: Selects all the files in the directory, i.e, File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt
The above output indicates that the files: File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt are compressed, resulting in the TarArchive.tar archive.
How to Compress Files to tar.gz Archive in Linux/Ubuntu Terminal?
The “tar.gz” is one of the most popular compressed archive file formats in Linux operating systems. “tar.gz” uses the “gzip” compression algorithm to compress data. Files are compressed to a tar.gz archive via the tar command. For example, to compress the files: File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt to tar.gz archive “TarGzArchive.tar.gz”, execute the following command:
$ tar -czf TarGzArchive.tar.gz *.txt
Where:
- c: Creates a new archive.
- z: Creates the archive using “gzip”.
- f: Write to the archive
- *.txt: Selects all the files in the directory, i.e, File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt
The above output indicates that the files: File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt are compressed, resulting in the TarGzArchive.tar.gz archive.
How to Compress Files to tar.xz in the Archive in Linux/Ubuntu Terminal?
The “tar.xz” is one of the newest compressed archive file formats for Linux operating systems. “tar.xz” uses LZMA/LZMA2 compression algorithms to compress data. Files are compressed to a tar.xz archive via the tar command. For example, to compress the files: File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt to tar.xz archive “TarxzArchive.tar.xz”, execute the following command:
$ tar -cJf TarxzArchive.tar.xz *.txt
Where:
- c: Creates a new archive.
- J: Creates the archive using “.xz”.
- f: Write to the archive
- *.txt: Selects all the files in the directory, i.e, File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt
The above output indicates that the files: File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt are compressed, resulting in TarxzArchive.tar.xz archive.
How to Compress Files to tar.bz2 in Archive in Linux/Ubuntu Terminal?
The “.tar.bz2” is a highly efficient compressed archive file format. It uses the BZ2 compression algorithm to compress data. Files are compressed to a tar.bz2 archive via the tar command. For example, to compress the files: File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt to tar.bz2 archive “TarbzArchive.tar.bz2”, execute the following command:
$ tar -cvjf Tarbz2Archive.tar.bz2 *.txt
Where:
- c: Creates a new archive.
- v: Produce verbose output.
- j: Creates the archive using “.bz2”.
- f: Write to the archive
- *.txt: Selects all the files in the directory, i.e, File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt
The above output indicates that the files: File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt are compressed, resulting in Tarbz2Archive.tar.bz2 archive.
How to Compress Files to .zip Archive in Linux/Ubuntu Terminal?
The “zip” is the most popular compressed archive file format due to its cross-platform-compatible nature. “Zip” uses the “DEFLATE” compression algorithm to compress data. Files are compressed to a zip archive via the zip command. For example, to compress the files: File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt to zip archive “ZipArchive.zip”, execute the following command:
$ zip ZipArchive.zip *.txt
Where “*.txt” selects all the files in the current directory, i.e, File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt.
The above output indicates that the files: File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt are compressed, resulting in the ZipArchive.zip archive.
How to Compress Files to .7z in the Archive in Linux/Ubuntu Terminal?
7z is the most efficient compressed archive file format and 7z delivers the highest compression ratio. Additionally, 7z is a multi-platform compressed archive file format. 7z is not installed in Ubuntu by default. To install 7z, execute the following command:
$ sudo apt install p7zip-full

The above output indicates that the p7zip-full package is successfully installed. Files are compressed to a .7z archive via the 7z command. For example, to compress the files: File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt to 7z archive “7zArchive.7z”, execute the following command:
$ 7z a 7zArchive.7z *.txt
Where:
- a: add files to the archive
- *.txt: Selects all the files in the directory, i.e, File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt
The above output indicates that the files: File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt are compressed, resulting in a 7zArchive.7z archive.
How to Compress Files to .rar in the Archive in Linux/Ubuntu Terminal?
The “.rar” (Roshal Archive) is a compressed archive file format that supports error correction and file spanning. rar is not pre-installed in Ubuntu. To install rar, execute the following command:
$ sudo apt install rar

The above output indicates that the rar package is successfully installed. Files are compressed to a .rar archive via the rar command. For example, to compress the files: File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt to rar archive “RaRArchive.rar”, execute the following command:
$ rar a RaRArchive.rar *.txt
Where:
- a: add files to the archive
- *.txt: Selects all the files in the directory, i.e, File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt
The above output indicates that the files: File001.txt, File002.txt, File003.txt, File004.txt, and File005.txt are compressed, resulting in RarArchive.rar archive.
How to Compress Files to Pigz in the Archive in Linux/Ubuntu Terminal?
Pigz is an efficient compressed archive file format for a single file only. Pigz is not pre-installed in Ubuntu. To install pigz, execute the following command:
$ sudo apt install pigz

The above output indicates that the “pigz” package is successfully installed. A file is compressed to a .pigz archive via the pigz command. For example, to compress a file: File001.txt, to a pigz archive “File001.txt.gz”, execute the following command:
$ pigz File001.txt

The above output indicates that the file, File001.txt is compressed to File001.txt.gz archive.
How to Compress Files to .gzip Archive in Linux/Ubuntu Terminal?
The “gzip” is a popular compressed archive file format that is used to compress files. Additionally gzip is used to compress tarballs and HTTP traffic before transmission. A file is compressed to a .gzip archive via the gzip command. For example, to compress a file: File001.txt, to a gzip archive “File001.txt.gz”, execute the following command:
$ gzip File001.txt

The above output indicates that the file, File001.txt is compressed to File001.txt.gz archive.
Conclusion
Files can be compressed into archives via tar, tar.gz, tar.xz, tar.bz2, zip, 7z, rar, pigz, and gzip-compressed archive file formats in Linux/Ubuntu. Among these methods, files are compressed with the highest efficiency via the 7z compressed archive file format. Additionally, pigz and gzip compressed archive file formats are used to compress a single file, while, tar, tar.gz, tar.xz, tar.bz2, zip, 7z, and rar compressed archive file formats are used to compress multiple files to archives. A user can use any compressed archive file format based on their requirements.