The tar (i.e., tape archive) command is used to convert a group of files into an archive (no compress).
Unlike some other archiving programs, and consistent with the Unix philosophy that each individual program should be designed to do only one thing but do it well, tar does not perform compression. However, it is very easy to compress archives created with tar by using specialized compression utilities.
The basic tar syntax is
tar option(s) archive_name file_name(s)
Remember certain option defined, c for create, z for extract and t for test. v verbose, and f is file. There are two common archive formats that people are interested in, tar.bz2 and tar.gz (tgz). tar.bz2 is more compress than tar.gz, but tar.gz is faster for creating and extracting.
Create and Compress
To create a tar.gz archive, if given list of files, use option z to indicate tar.gz:
tar czvf filename.tar.gz myfile1 myfile2 myfile3
To create a tar.gz archive from a folder, if folder must be included in the archive, that means will be extracted with the folder too:
tar czvf filename.tar.gz foldername/*
By default, tar creates an archive of copies of the original files and/or directories, and the originals are retained. However, they can be removed when using tar by adding the –remove-files option.
Extract and List
To test the tar file to see the content of the tar.gz file, use t option.
tar tzvf filename.tar.gz
To extract the tar.gz file, use x option.
tar xzvf filename.tar.gz
To extract the tar.gz file to a specified folder
tar xzvf filename.tar.gz -C foldername/
tar.bz2
For tar.bz2, use the same option but change everything from z to j, for example:
tar cjvf filename.tar.bz2 myfile1 myfile2 myfile3
tar cjvf filename.tar.bz2 foldername/*
tar tjvf filename.tar.bz2
tar xjvf filename.tar.bz2