How to Archive and Compress and Back Again (using zip, gzip, bzip2, and tar in terminal)

File ArchiverPlenty of linux users enjoy working from the command line; from manipulating files to compiling software, many users never seem to leave the terminal. Something that I do all the time at the terminal is archiving and unarchiving files as well as compressing them. Today I’ll demonstrate the necessary commands to do this using a few of the most common compression schemes so that you too can handle archives like the pros.

Footnote: If you know what you’re doing already, you might like the cheat-sheet better!

We’ll start with compressing a single file because that’s the easiest thing to do and we’ll work our way to archiving and compressing directories.

Gzip Files

Creating a gzipped file is as simple as typing gzip followed by the filename:

$ gzip bank_statement

This will output a file with a gz extension indicating it has been compressed: bank_statement.gz. Simple enough.

Decompressing a gzipped file is just as simple using the gunzip command followed by the filename:

$ gunzip bank_statement.gz

This will output the original file without the gz extension indicating it has been decompressed: bank_statement.

Bzip2 Files

Creating a bzipped file works identically to gzip; simply type bzip2 followed by the filename:

$ bzip2 bank_statement

This will output a file with a bz2 extension indicating it has been compressed: bank_statement.bz2.

Decompressing a bzipped file works just like you would expect. Use the bunzip2 command followed by the filename:

$ bunzip2 bank_statement.bz2

This will output the original file without the bz2 extension indicating it has been decompressed: bank_statement.

Tar Archives

Creating a tar archive allows you to group multiple files or directories into a single file. Windows users might find this similar to a zip file. One difference is that tar simply groups files together but does not compress them the way zip does. To create a tar archive we will use the tar command, the filename of the archive to create, and the files or directories you want to archive.

Archiving a Single File

To archive a file you would use a command similar to this:

$ tar cf bank_statement.tar /home/user/bank_statement

This will output a file named bank_statement.tar that contains /home/user/bank_statement. The flags used here are:

  • c: create an archive
  • f: specifies that the argument following is the filename for the archive

The file to archive doesn’t have to be the full path; you can just as easily use these commands to change to the /home/user directory and tar the bank_statement:

$ cd /home/user
$ tar cf bank_statement.tar bank_statement

Archiving Multiple Files or Directories

Typically you wouldn’t want to tar a single file though. It’s more useful to tar multiple files or directories like the following respective commands:

$ tar cf some_important_documents.tar bank_statement tax_document
$ tar cf all_important_documents.tar /home/user/important_documents/

The first command will output a file named some_important_documents.tar that contains both bank_statement and tax_document, while the second command will output a file named all_important_documents.tar that contains the entire contents of the important_documents directory. Now we’re getting somewhere!

Unarchiving

Unarchiving a tar file uses the tar command followed by the archive file name. Instead of the c flag, we use x instead:

$ tar xf some_important_documents.tar

This will create the original files that were stored in the tar archive; in this case both bank_statement and tax_document would be output to the file system. The new flags used in that command are:

  • x: extract an archive

Remember that the f is used to specify the archive filename.

Gzipped Tar Archives

Creating a gzipped tar archive is very similar to creating a zip file in the Windows world because you achieve a compressed archive file that contains many other files. To create a tar archive and simultaneously gzip it, pass the z flag to tar:

$ tar czf all_important_documents.tar.gz /home/user/important_documents/

This will output the file all_important_documents.tar.gz which is a gzip compressed archive of the entire contents of the important_documents directory. The new flags used in that command are:

  • z: apply gzip when creating an archive and gunzip when extracting an archive

Decompressing and extracting a gzipped tar archive is easy once you know how to create one. To reverse the process just swap the c flag for the x flag just like before:

$ tar xzf all_important_documents.tar.gz

This will create the original files that were stored in the tar archive; in this case the important_documents directory would be output to the file system.

Bzipped Tar Archives

Creating a bzipped tar archive works just like the gzipped tar archive; just pass the j flag to tar instead of the z flag:

$ tar cjf all_important_documents.tar.gz /home/user/important_documents/

This will output the file all_important_documents.tar.gz which is a bzip2 compressed archive of the entire contents of the important_documents directory. The new flags used in that command are:

  • j: apply bzip2 when creating an archive and bunzip2 when extracting an archive

Decompressing and extracting a bzipped tar archive works the same, of course:

$ tar xjf all_important_documents.tar.gz

This will create the original files that were stored in the tar archive; in this case the important_documents directory would be output to the file system.

Zip Files and Archives

Creating a zip file will seem suprisingly simple after reading about tar since it handles the compression automatically without you having to pick one.

Zipping a Single File

To zip a single file you just use the zip command followed by the archive filename and then the file you want to zip:

$ zip bank_statement.zip bank_statement

This will output a file named bank_statement.zip that contains bank_statement.

Zipping Multiple Files

To zip multiple files you just add more filenames after the archive name:

$ zip some_important_documents.zip bank_statement tax_document

This will output a file named some_important_documents.zip that contains both bank_statement and tax_document.

Zipping Directories

To zip a directory you can use the -r flag:

$ zip -r all_important_documents.zip important_documents

This will output a file named all_important_documents.zip that contains the entire contents of the important_documents directory.

Extracting Zip Files

To unzip a zip file, just use unzip followed by the filename:

$ unzip all_important_documents.zip

This will create the original files that were zipped; in this case the important_documents directory.

Additional Tips

And that’s all you need to know about archiving and compressing in linux (well, maybe not everything)! Here a few more tips that I find useful day-to-day:

  • Before unzipping a file, try using the -l flag to view the contents of the archive first:
    $ unzip -l unknown_zip_archive.zip
  • Before untarring a file, try using the t flag to view the contents of the archive first:
    $ tar tzf unknown_gzip_tar_file.tar.gz
  • You might see gzipped tar files with the extension tar.gz or simply tgz. There is no difference, they are both gzip compressed tar files.
  • You might see bzipped tar files with the extension tar.bz2, tb2, or tbz2. There is no difference, they are all bzip2 compressed tar files.
  • For more information when tarring and untarring archives (such as what files are going into the archive), try using the v flag to enable verbose output:
    $ tar cvzf backup.tar.gz /home/user