Back to Basics Part 5 -- Working with archives in GNU/Linux
So, last time we learned to use the grep command to search through documents, searches, processes, etc, but what about working with archives? What good is working from the commmand line, if you get stuck the moment you have to unpackage something?
Thankfully, I actually find it incredibly simple to extract and package things via terminal in comparison to using something like Ark.
Itâ€™s come to the point where Iâ€™ll tend to extract things via terminal, after downloading them from Firefox, rather than clicking through Firefox / my archive manager for two minutes, when I can extract it in about 10 seconds myself.
Zip files have got to be the easiest thing to work with, in the world... In my opinion. Letâ€™s assume that for the duration of these examples, we want to work with packages called example1.zip example2.zip etc.
- To extract: unzip example1.zip
- To extract all the archives in a directory:Â unzip â€˜*.zipâ€™
- To change the desired output directory: unzip example1.zip -d /path/to/new/directory/
- To list the contents of an archive:Â unzip -l example1.zip
- compressing one file:Â zip example2.zip mytextdocument.txt
- To compress multiple files: zip example2.zip mytext.txt myothertext.txt mythirdfile.txt
- Wildcard zipping:Â zip example2.zip * #be careful with this one. This will put all loose files in the current directory, into example2.zip... Unless you wanted to do that.
Working with tarballs
Another very popular archive type in the GNU/Linux world, are tarballs, however they can be a little more confusing at first to new users.
Tarballs generally come in three flavours:
- .tar â€“ A generic tarball
- .tar.gz â€“ A tarball made with the program gzip
- .tar.bz2 â€“ A tarball made with the program bzip2
Working with these files is all basically the same for the three, with just minor differences of a change in options used with your command.
Extracting:Â tar -xf example1.tar
- -x is used to state we want to extract
- -f is used to specify the name of the archive we want to decompress
Compressing: tar -cf example1.tar a.txt
This will create .tar, putting the a.txt file inside the archive. You can follow the same concept as with others, adding multiple file names, or by using the * wildcard, to add multiple files, or specify the directory you wish compressed instead.
.tar.gz and .tar.bz2
When working with .tar.gz or .bz2 archives, you must add a different option depending on whether you wish to use gzip (generally faster, but larger filesizes) or bzip2 (generally slower, but more compressed). You may access the tar manual on the Gnu website.
- When extracting and compressing a .tar.gz file, you must add the -z option
- When extracting and compressing a .tar.bz2 file, you must add the -j option
- Compress files: tar -cjf example4.tar.bz2 a.txt b.txt
- Extract an archive: tar -xzf example4.tar.gz
- Add Files/Directories to Tar archives: tar -rvf example4.tar test.txt
- Verify a tar archive: tar -tvfW example4.tar
- List content of a tar archive: tar -tvf example4.tar
- Untar single file from root of tar archive: tar -xvf example4.tar myfile.txt
- Untar single file from root of tar.gz archive: tar -zxvf example4.tar.gz myfile.txt
- Untar single file from root of tar.bz2 archive: tar -jxvf example4.tar myfile.txt
- Untar multiple files from archive:Â tar -xvf example4.tar "myfile.txt" "myfile2.txt"
The options: c=create, f=file, x=extract, r=append, W=verify
Lastly, if you want a more verbose (detailed) output for all of the above, add v to the end of your options before f:
- tar -xzvf example5.tar.gz
Archives are incredibly easy to handle, and with time and patience your fingers will fire off on their own over time and work magic before your eyes with little effort. Happy hacking!