How to compress, archive and uncompress files and directories using tar

To compress, archive and uncompress (extract) files and directories in Unix, we can use the tar command. Following is an example of the tar command where we are going to compress and archive a directory called test and then uncompress using the same command.

To compress and archive:

tar -zcvf nameoffiletocreate.tar.gz /path/to/the/directory/called/test

Here c is for compressing using the gzip program and v is for vebrose

To uncompress and extract the archive:

tar -zxvf nameoffilethatwascreated.tar.gz

Here, x is for xpand and v is for vebrose