Back to Basics Part 5 -- Working with archives in GNU/Linux - gHacks Tech News

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

Linux archives commandline

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

Examples:

  • 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

Final Thoughts

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!

Related articles

Summary
Back to Basics Part 5 -- Working with archives in GNU/Linux
Article Name
Back to Basics Part 5 -- Working with archives in GNU/Linux
Description
Mike talks about working with archives -- zip, tar, tar.gz and tar.bz2 -- on Linux systems using the Terminal. Learn all important commands and options to extract archives on Linux.
Author
Publisher
Ghacks Technology News
Logo

We need your help

Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.

If you like our content, and would like to help, please consider making a contribution:

Comments

  1. 420 said on March 15, 2018 at 1:32 pm
    Reply

    This should be called working with archives in bash, because when I do stuff with archives I use engrampa. I guess some people prefer to do everything in the command line but for archive management I prefer a gui.Thank you though for showing me.

  2. Mo said on March 15, 2018 at 1:55 pm
    Reply

    Love these back to basics Linux tutorials. Please keep them coming.

  3. dark said on March 15, 2018 at 11:50 pm
    Reply

    Most Linux distros also come with GUI Archive Manager.

  4. summer said on March 16, 2018 at 9:40 am
    Reply

    Great

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

Please note that your comment may not appear immediately after you post it.