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
tar -cvf [archive.tar] [directory to archive]
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:
tar -tvf [archive.tar]
What can also be useful later is to update the archive or add new files to it. For that,
tar -rvf [archive.tar] [directory or file to add]
will add a new file or directory to the archive, while
tar -uvf [archive.tar] [modified directory]
will update changes made in the original directory to the tar file. Finally, last but not least, to extract a tar file, type
tar -xvf [archive.tar]
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
unrar x [archive.rar]
I guess you might have to list the compressed files with
unrar v [archive.rar]
Or even test the archive integrity with
unrar t [archive.rar]
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
zip -rv [archive.zip] -o [directory to archive]
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:
7z a [directory to compress]
in order to compress a directory (how did you guess?)
7z 1 [archive.7z]
to list the content of the archive
7z u [archive.7z] [file to add or updated directory]
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
7z x [archive.7z]
Another proprietary archive type, ace behaves similarly to rar:
unace x [archive.ace]
to extract an archive.
unace v [archive.ace]
to list the content of the archive
unace t [archive.ace]
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:
alias untar="tar -xvf"
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.