+3 votes
263 views
Compressing files on Linux from the console

in Guides by (552k points)
reopened | 263 views

1 Answer

+4 votes
Best answer

Basic guide on compressing files on Linux from the console.
Main compressors, filing cabinets and their formats.
Basic commands to compress and decompress the different formats in Linux.

Welcome to the world of compression in the GNU / Linux universe , something relatively simple but that takes on some complexity when we are new users. If we also limit ourselves to the use of the console, the challenge can be much greater, but with this tutorial you will acquire the bases to unfold yourself with ease.

image

Currently, there are countless compressors, algorithms, packers, decompressors, etc. Although all of these generally have the same purpose, their use, functions or performance may vary. For this reason it is good to know the basic compression tools and their handling, so it will not take us by surprise.

Basic guide on compressing files on Linux from the console.

Within the compression / decompression tools available for Linux , there is a great variety, but we must not confuse a tool with a compression algorithm . A compression algorithm is a data coding system, which allows to represent a certain amount of information using a smaller space than originally used.

We must also differentiate a filing cabinet from a compressor. A compressor is simply able to compress a file, while an archiver can store a directory structure and can also allow data compression. To understand it better, with a compressor we can only compress one file, while with an archive we can compress files, folders and directory structures.

Main compressors, filing cabinets and their formats.

When we reach a GNU / Linux operating system and start downloading files from the internet, we will find many extensions and formats, but the most common are:

  • Zip : it is an extension known to all, since it has been used for many years and is present in virtually any operating system. This format is recognized by the .zip file .zip .
  • Gzip : it is a format whose name comes from GNU Zip, but we must not confuse it with Zip, since they are different and incompatible formats. Gzip can only compress files, but cannot archive them, so it is usually used normally with the tar archive. Files compressed with Gzip have the extension .gz and when used in combination with tar they usually have the extension tar.gz or .tgz .
  • bzip2 : The bzip2 format offers greater compression than Gzip, but also uses more memory and time to compress / decompress . Bzip2 uses the extension .bz2 , but it is usually used in combination with the archiver tar resulting in the extension .tar.bz2 , although we can also find extensions such as .tbz , .tbz2 or .tb2 .
  • xz : this format is one of the most modern and its use has spread widely throughout the network. It offers excellent compression ratios and we have published some time ago a tutorial on how to unzip .tar.xz files on Linux. This format uses the .xz extension, but is usually used with the tar archive so the most common extension is .tar.xz or .txz .
  • rar : a format of the best known and used in the network to distribute and share files. This format compresses and also archives, but it is not free software , so in some Linux distributions the installation of additional programs will be necessary. The file extension for this format is .rar .
  • 7zip : this is a free file archiving tool that uses the 7z format with the LZMA and PPMD "‹"‹compression algorithm. This archive supports a multitude of compression formats and its extension is .7z .
  • lzma : refers to the LZMA compression algorithm and can be used in combination with the tar archive. We can find files with the extension .lzma or .tar.lzma , but they are rare extensions.
  • lzip : this is a free program that uses the LZMA compression algorithm. Its file extension is .lz but it is usually used together with tar originating the file extension .tar.lz or .tlz .
  • compress : is a tool that currently does not have much use and that uses the modified Lempel-Ziv (Lempel-Ziv-Welch) algorithm . The default file extension of compress is .z but as it can be used with tar, we can also find the extension .tar.z , .taz or .tz .
  • tar : as we said before, it is an archive and by itself this tool does not compress the data . This archive was designed for Unix environments and uses the .tar file extension.

image

Basic commands to compress and decompress the different formats in Linux.

In this section we will give the basic commands so you can unzip compressed files or compress your own files and folders.

TAR.

Clarify that in this case tar will not use any compression and the commands would be:

  • Compress: tar cvf paquete.tar /home/carpeta
  • Unzip: tar -xvf paquete.tar

ZIP

To compress zip files from the console it is usually necessary to install the unzip tool. In Ubuntu we can install it with the sudo apt-get install unzip command. Then to unzip a zip file we will have to use the unzip archivo.zip command.

Command to compress files: zip paquete.zip /home/carpeta

Gzip

If we use Gzip next to the tar tool, we can use the following commands:

  • Compress: tar -zcvf paquete.tar.gz /home/carpeta
  • Unzip: tar -zxvf paquete.tar.gz

Bzip2.

The use of bzip2 is generally next to tar, so the commands would be:

  • Compress: tar -jcvf paquete.tar.bz2 /home/carpeta
  • Unzip: tar -xvf paquete.tar.bz2

XZ

In this case the commands would be:

  • Compress: tar -Jcvf paquete.tar.xz /home/carpeta
  • Unzip: tar -xvf paquete.tar.xz

RAR

To unpack rar files the unrar tool is used , on which we have already written a complete tutorial on how to unzip rar files in Ubuntu and how to install this tool.

7zip

This tool supports multiple file formats, but to use it with the 7z format we can use the following basic commands:

  • Compress: 7z a paquete.7z /home/carpeta
  • Unzip: 7z e paquete.7z

Compress

This compressor is usually used with tar and the basic usage commands are:

  • Compress: tar -cZvf paquete.tar.z /home/carpeta
  • Unzip: tar -xvf paquete.tar.z

LZMA

Generally we will see that it is used with tar files and the commands are:

  • To compress: tar -cvf paquete.tar.lzma --lzma /home/carpeta
  • To unzip: tar -xvf paquete.tar.lzma

Lzip

We will need the Lzip tool installed to compress and decompress these files, so we will have to install it. In Ubuntu it is as simple as executing the sudo apt-get install lzip command.

Once the tool is installed, just use the following commands:

  • Compress: tar -cvf paquete.tar.lz --lzip /home/carpeta
  • Unzip: tar -xvf paquete.tar.lz

As you can see, there are many compression formats, but we can use tools such as DTRX that facilitate the task.


by (3.5m points)

Related questions

+5 votes
1 answer
asked Aug 9, 2019 in Guides by backtothefuture (552k points) | 223 views
+5 votes
1 answer
+3 votes
1 answer
+5 votes
1 answer
+4 votes
1 answer
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users