Simply put, archiving is storing files and directories in a single file called 'archive'. Extracting is getting those files and directories from that single file. Quite often compressing is used together with archiving, and that is the process of reducing the size of the files. Basically, we use archiving and compressing to transfer files from one place to another and back up the data. You probably agree that it's easier and more convenient to download, move or store one small file than many large ones.
zip and unzip
If you worked in OS Windows before, you've definitely met some files with .zip extension. Those are zip-compressed archives. They can also be used in Unix-like systems.
Creating zip archive from one file or many files in the terminal looks like this:
zip <zipfilename> <filename1> <filename2>
After zip put the name of the archive and the name of the files in your current directory or a path to those files. For example, let's make an archive from two text files.
zip my-archive my-report.odt my-report.doc
This command will create a compressed archive my-archive.zip with two files inside.
In case you need to archive and compress directories use the option -r (recursive) with the command:
zip -r <zipfilename> <directoryname>
Extraction of the files from the archive is even easier than archiving them in the first place:
unzip <zipfilename.zip>
unzip by default extracts files to the current directory.
So extracting files from our archive my-archive.zip will look like this:
unzip my-archive.zipgzip and gunzip
However, in Unix-like systems gzip is the most popular tool for compressing files. Files compressed with this tool have a .gz extension.
Let's start by taking a look at how to compress a file with gzip. It is very simple:
gzip <filename>
For decompressing use this gunzip command, it's also quite easy:
gunzip <filename.gz>
Just like before with zip, if you need to compress files in a directory, you need the -r option:
gzip -r <directory-name>
zip command, gzip will not compress the directory, but only files inside it! If you want to compress the whole directory, you should use the tar command.
tar
tar, short from "Tape ARchive", is an archiving tool in UNIX-like systems that can also use compressing, for example, gzip-compressing. The name originated from the magnetic tapes that were used for storing data. The tar files have extension .tar, and files compressed with gzip have .tar.gz or .tgz extensions. You might even come across the informal name of those archives – "tarballs".
tar has really a lot of options, that's why we made life a little bit easier for you and chose the most useful ones. Look at how you can create a compressed archive from a directory:
tar czvf <filename.tar.gz> <directory>
Let's review the options we used closely:
c means create an archive;
z – use gzip for compressing;
v – verbose output that will give you the information on files that will be processed;
f means that the output is the file and not a device (for example tape drive). If you don't use it, tar will throw an error.
tar.
Now let's take a look at how we can archive our home directory:
tar czvf home-archive.tar.gz /home/
When you need to extract the files from the archive use these options:
tar xzvf <filename.tar.gz>
The difference here is in the x option, which means extract. Also, here z means gunzip, not gzip since we are extracting files.
Conclusion
To be brief, gzip is compressing tool, tar and zip are archiving and compressing tools. You can use them when you need to transfer or backup files. We recommend using gzip or zip when you want to compress a file, and tar or zip if you want to compress or archive more than one file or directory. If you need to extract or decompress files look at the extensions: if it's .zip, then run unzip command, with .gz run gunzip; with .tar, .tar.gz, .tgz tar will be of use.