TAR Stands for Tape Archive one of the top essential Linux command that every System Administrator should aware. TAR command creates an archive of multiple file and folder into single archive file. What is the different between archive and compress. Archive is a collection of files and folders in one file. This helps to take a backup, copy, move from one system to another system with ease. Archive is not a compressed one but a compressed file can be archive. Archive file takes a same amount of space what all individual files and directories have taken. A Compressed file is collection of a file and directories stores in a single file which uses less disk space. Within a GNU tar command compression is integrated. TAR command can create a tar archive and then compress with gzip or bzip2 compression algorithm and we normally refer them as a “tarball“. In this article, we will discuss some of the tar commands which are very useful for daily operations.
1. Archive and Compress a Directory.
Following command archive and compress a test directory and tar a single file name test.tar.gz
# tar -zcvf test.tar.gz test/
Meaning of those above options used with tar command.
2. Extract compressed file.
Following command extract compressed file.
# tar -xvf test.tar.gz
-x : Extract compressed file
-v : Verbose (you can see the progress of the command in the terminal)
-f : Specify file name for archive
3. Archive and compress multiple directories.
In our test case we have directories called test/ test1/ test2/ having multiple files in it. Following command creates a single compress file of multiple directories and files. It will create a test.tar.gz file of those directory.
# tar -zcvf test.tar.gz test/ test1/ test2/
4. Extracting file to a particular directory /tmp.
You can extract the compressed file to a particular directory using “-C” switch specifying the directory. Following command extract test.tar.gz to /tmp directory
# tar -xvf test.tar.gz -C /tmp/
5. List a file and directory from tar archive file.
Following command with “-t” option to view the contains of tar archive file test.tar.gz
# tar -tvf test.tar.gz
6. Create a tar archive file or tarball without compression.
Following command create a test.tar archive file only for test directory.
# tar -cvf test.tar test/
7. Add or append one more directory test1 in tar archive.
Following command append test1 directory in test.tar archive file. Below screenshot shows the appended directory test1 also.
Note: You cannot append or add file and directory in compressed tar archive file.8. bzip2 compression
TAR also support bzip2 compression and often named .tat.bz2 file. Replace “-z” options with “-j” for bzip2. It’s slower than gzip but compress more to get a smaller file size. You can extract with same command what we have seen in point no. 4