An archive file is nothing but a collection of files and directory stored in one file. A compressed file uses less disk space. This quick page explains how to compress, list and extract filesTAR
The tar command is not specifically a compression command. It’s generally used to pull a number of files into a single file for easy transport to another system or to back the files. It also provides compression as a feature, which makes a lot of sense, and the addition of the z compression option is available to make this happen.
When compression is added to a tar command with the z option, tar uses gzip to do the compressing.
Compress an Entire Directory or a Single File
Use the following command to compress an entire directory or a single file on Linux. It’ll also compress every other directory inside a directory you specify–in other words, it works recursively.
-securelve_sh-4.1$ tar -czvf name-of-archive.tar.gz /path/to/directory-or-files
Here’s what those switches actually mean:
-c: Create an archive.
-z: Compress the archive with gzip.
-v: Display progress in the terminal while creating the archive, also known as “verbose” mode. The v is always optional in these commands, but it’s helpful.
-f: Allows you to specify the filename of the archive.
Let’s say you have a directory named “stuff” in the current directory and you want to save it to a file named archive.tar.gz. You’d run the following command:
-securelve_sh-4.1$ tar -czvf archive.tar.gz stuff
Or, let’s say there’s a directory at /domain/public_html/something on the current system and you want to compress it to a file named archive.tar.gz. You’d run the following command:
-securelve_sh-4.1$ tar -czvf archive.tar.gz /domain/public_html/something
Extract an Archive
Once you have an archive, you can extract it with the tar command. The following command will extract the contents of archive.tar.gz to the current directory.
-securelve_sh-4.1$ tar -xzvf archive.tar.gz
You may want to extract the contents of the archive to a specific directory. You can do so by appending the -C switch to the end of the command. For example, the following command will extract the contents of the archive.tar.gz file to the /tmp directory.
-securelve_sh-4.1$ tar -xzvf archive.tar.gz -C /tmp
zip is a shell command to create zip archive files.
Creating a zip archive from a directory
The command line option -r adds files recursively. Thus, it allows to create a zip file from an entire (sub-)directory:
-securelve_sh-4.1$ zip dir.zip -r directoryname-to-be-zipped
The -r also adds hidden file (files whose name starts with a dot).
Showing a zip files content (unzipping)
The content of a zip file can be shown with unzip and the command line option -l:
-securelve_sh-4.1$ unzip -l dir.zip
Unzipping into a specific directory
-d allows to specify a directory into which the contents of a zip file should be extracted.
The following command extracts dir.zip into the directory /tmp (and creates /tmp/dir):
-securelve_sh-4.1$ unzip dir.zip -d /tmp
Make sure you are on the correct working directory to execute all of the above commands.