There are plenty of graphical solution for dealing with archives (or zipped files) in Linux, but none of them beats the command line for the speed and versatility. The only bad thing about it is that there are so many different types of archives, each with its own specific syntax and properties and it can become very hard to deal with all of them without a little preparation. To deal with this, I offer you a list of the most common archive types (proprietary and open ones) with their basic syntax.
Probably one of the most common archives, tar is free and cool. To create and archive, the syntax is
Here, “c” is to create a new archive, “v” for verbose mode, and “f” to give a name to the created archive.
But tar archives can also tag along with gzip in order to compress the file. Simply add the option “z” to the previous line, and you will create a .tgz or .tar.gz file. If you want to see the files in a tar archive, use the command:
What can also be useful later is to update the archive or add new files to it. For that,
will add a new file or directory to the archive, while
will update changes made in the original directory to the tar file. Finally, last but not least, to extract a tar file, type
where “x” stands for “extract.”
Rar is the evil twin proprietary brother of tar (at least the name makes it sound like it). The only thing you should do with rar files on Linux is extract them with the command
I guess you might have to list the compressed files with
Or even test the archive integrity with
Note that the package “unrar” must be installed beforehand.
Also very popular, zip compresses and archives at the same time. However, it is a little bit more complex to use in the command line than tar. The syntax to create a new zip archive is
Here, “v” is again for verbose mode, “o” is to indicate the directory to archive, but “r” stands for “recursive,” which serves to keep the file structure intact. It is also possible to update the archive by adding “u” with the first set of options. However, decompressing remains simple:
With a higher level of compression, bzip2 can be preferable to gzip, but the options are not very extended and it is only possible to compress one file (and not a directory). Use
to compress and
Not installed by default, p7zip remains very powerful at compressing at high rates. It also contains as many options as tar:
in order to compress a directory (how did you guess?)
to list the content of the archive
will add a new file or directory to the archive, or even update the changes made in the original directory.
Finally, to extract the archive, the syntax is very close to that of unrar
Another proprietary archive type, ace behaves similarly to rar:
to extract an archive.
to list the content of the archive
to test its integrity.
Once you’ve mastered all archive types, you may realize that there are a few tips that should always be kept in mind. First, be considerate of others when you create an archive. Don’t you hate it when you extract something and all newly created files mix with the ones which were already in the directory? You lose time and patience cleaning everything. So anticipate the extraction and always archive a single file or directory so that nothing will burst out.
Furthermore, you may have noticed that every command for extracting an archive is pretty simple, except the one for tar. So here is my advice, create an alias:
That way, the commands for extracting are untar, unrar x, unzip, bunzip2, 7z x, unace x. Straightforward right? And if you want to always keep the alias, don’t forget to copy-paste the previous line in your .bashrc file in your home directory.
Finally, I said that bzip2 had a good compression, but it did not work for directories. You probably guessed that it is not a problem if you use another archive type before. Simply archive the directory that you want, with tar for example. Then compress it with bzip2, as it will be considered a single file.
What else can I say? Archives are handy, fast, and easy. Once you’ve learned the basic manipulations, compression will soon keep no secrets from you. And compiling programs will become super easy, entirely via the console. Of course, now that you know which types are free, I encourage you to use them instead of the proprietary ones.
Do you know of some other types of archives? Which one do you prefer? Why? Or is there a missing command/functionality that should be added here? Please let us know in the comments.
Image credit: Yellow Computer Folder With Zipper by Big Stock Photo.