Archive, Compress and Extract Directories and Files Using TAR Command in Linux

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.

Also Read:-

Linux commands Part-1
Linux commands Part-2
Linux commands Part-3
Linux commands Part-4
Linux commands Part-5

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. 

-z : Compress the archive with gzip
-c : Create archive
-v : Verbose (you can see the progress of the command in the terminal)
-f : Specify file name for archive

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

-z: use gzip compress
-j: use bzip2 compress
# tar -jvcf test.tar.bz2 test/

9. Untar single file from tar bz2 archive. 
List archive file as shown in point no. 5. and following command  extracts teclues2.log file in test directory.
# tar –extract –file=test.tar.bz2 test/teclues2.log
10. Excluding or ignoring particular file type while creating tar archive file.
With –exclude option with tar command to exclude the particular file type. Following command exclude *.log file while creating test.tar file.
That’s it. In this post we have seen usage of TAR command with examples. Please share and forward the post with others if you liked it.

3 thoughts on “Archive, Compress and Extract Directories and Files Using TAR Command in Linux”

Leave a Comment