File compression is great utility when reducing the space taken up by files and others documents. In this way, although all the information is preserved, the size is reduced.
This happens thanks to the fact that the software in charge of this task, performs a search for repetitions within codes of each element, and then store only this series together with the times it is repeated. For example, if it appears “PPPPPPP” (6 bytes), I would change it to 6P (2 bytes).
For its part, Linux has since its inception, a compression tool called Tar. Over time, other softwares with similar functions have appeared. However, all of them can be managed from the console. To learn more about this, we recommend the following post.
What are the benefits of compressing files with the Linux console?
As we mentioned before, all programs created to compress files, developed to Linux serve similar functions. In addition, each one has own graphical interfaces where the user can interact and control the functions. There are even other tools capable of managing all of them from a single window.
However, one of the peculiarities of this system, and that makes it so interesting for users, is the ability to perform actions from the command line. This option is the most convenient for save resources that occupy the program’s own interfaces. In the same way, considerable time can be saved, since the commands are quite simple.
Learn step by step how to compress files and folders from the Linux console
The programs most used by users to compress individual files are Gzip, Bzip2, P7zip among others. The downside of many of them is that they are not able to do the work on complete files. It is because of that Tar It is so important. Since it is the perfect complement to achieve this goal.
We will see below how to perform compression in both cases:
Command syntax it is very similar in all programss of this type. For example, if we use Bzip2, to compress a file in a standard way we should write the following command. “Bzip2 example.txt” Where example.txt is the name of the document. This will create a new file .bz2 and will delete the original. In the case of wanting to compress different files from the same command, just name them all in the following way “Bzip2 example1.txt example2.txt example3.txt”.
Other possible configurations for this program are made by adding the following values in the command after “bzip2”:
- Values between “-1” Y “-9” indicate the degree of compression. Being 1 the smallest. If none are added, the default is 5.
- “-F” overwrite the compressed file with the same name if it exists.
- “-C” create the .bz2 in the standard location.
- “-Q” blocks all of the notifications broadcast by the program.
- “-T” make a verification of the file before compressing it.
- “-K” prevents it from being removed the original document after creating the tablet.
- “-H” opens the menu with all Available options.
The best tool to achieve this is Tar, but along with some of the others that we mentioned before. Depending on this factor, the extensions of the compressed files can be “.Tar.gz (.tgz)”, “.Tar.bz2 (.tbz2)”, etc. What happens in this process is that Tar it is responsible for joining all the files of the selected file without modifying them, and then compressing it in the usual way by the specific program.
That is, if we did not use the second software, Tar it would just create an uncompressed package. To perform this action just write the command “Tar -cf filename.tar example1.txt example2.txt”.
Steps to unzip a file from the Linux console
To unzip a file it is only necessary change some values.
As we will see below:
Individual files |
Continuing with the example of bzip2, you need to add “-D” to command. In this way it would be “Bzip2 -d example1.txt”.
Similarly, to unpack a full archive, you need to replace “C” by “X” within the command. So that the syntax is as follows “Tar -xf filename.tar”.
If you have any questions, leave them in the comments, we will answer you as soon as possible, and it will also be of great help to more members of the community. Thank you! 😉