Tar Command Examples in Linux

The Linux tar stands for tape archive, which is used by large number of Linux/Unix system administrators to deal with tape drives backup. The tar command used to rip a collection of files and directories into highly compressed archive file commonly called tarball or tar, gzip and bzip in Linux. The tar is most widely used command to create compressed archive files and that can be moved easily from one disk to anther disk or machine to machine.

In this article we will going to review and discuss various tar command examples including how to create archive files using (tar, tar.gz and tar.bz2) compression, how to extract archive file, extract a single file, view content of file, verify a file, add files or directories to archive file, estimate the size of tar archive file, etc.

The main purpose of this guide is to provide various tar command examples that might be helpful you to understand and become expert in tar archive manipulation.

Create Archive

1. Create tar Archive File

The below example command will create a tar archive file tbc-04-05-16.tar for a directory /home/tbc/files in current working directory. See the example command in action.

# tar -cvf tbc-04-05-16.tar /home/tbc/files

Let’s discuss the each option we have used in the above command for creating tar archive file:

  • c – Creates a new .tar archive file.
  • v – Verbosely show the .tar file progress.
  • f – File name type of the archive file.


2. Create tar.gz Archive File

To create a compressed gzip archive file we use the option as z. For example the below command will create a compressed MyFiles-04-05-16.tar.gz file for the directory /home/tbc/MyFiles. (Note : tar.gz and tgz both are similar).

# tar cvzf MyFiles-04-05-16.tar.gz /home/tbc/MyFiles
# tar cvzf MyFiles-04-05-16.tgz /home/tbc/MyFiles
  • z – Create a compressed gzip archive file.


3. Create tar.bz2 Archive File

The bz2 feature compress and create archive file less than the size of the gzip. The bz2 compression takes more time to compress and decompress files as compared to gzip which takes less time. To create highly compressed tar file we use option as j. The following example of command will create a MyFiles.tar.bz2 file for a directory /home/tbc/MyFiles. (Note: tar.bz2 and tbz is similar as tb2).

# tar cvfj MyFiles.tar.bz2 /home/tbc/MyFiles
# tar cvfj MyFiles.tar.tbz /home/tbc/MyFiles
# tar cvfj MyFiles.tar.tb2 /home/tbc/MyFiles
  • j – Create highly compressed tar file.


4. Create archive but exclude some files

If we want to create tar archive of a directory but needs to exclude some files, then following commands do this.

## Exclude single file ##
# tar -cvf MyFiles.tar /home/tbc/MyFiles --exclude='file1.php'

## Exclude multiple files ##
# tar -cvf MyFiles.tar /home/tbc/MyFiles --exclude='file1' --exclude='file2'

## Exclude pattern ##
# tar -cvf MyFiles.tar /home/tbc/MyFiles --exclude='file1' --exclude='patter*'

## Exclude directory ##
# tar -cvf MyFiles.tar /home/tbc/MyFiles --exclude='mydir/'
  • –exclude – Exclude file/directory while creating archive.


Untar/Extract Archive

5. Untar (extract) tar Archive File

To untar or extract a tar file, just issue following command using option x (extract). For example the below command will untar the file MyFiles.tar in present working directory. If you want extract in a different directory then use option as -C (specified directory).

## Untar files in Current Directory ##
# tar -xvf MyFiles.tar

## Untar files in specified Directory ##
# tar -xvf MyFiles.tar -C /home/tbc/ExtractedFiles/
  • x – Extract tar archive file.
  • -C – Extract in a different directory.


6. Uncompress tar.gz Archive File

To Uncompress tar.gz archive file, just run following command. If would like to untar in different directory just use option -C and the path of the directory, like we shown in the above example.

# tar -xvf MyFiles.tar.gz


7. Uncompress tar.bz2 Archive File

To Uncompress highly compressed tar.bz2 file, just use the following command. The below example command will untar all the files from the archive file.

# tar -xvf MyFiles.tar.bz2


Untar Single File

8. Untar Single file from tar File

To extract a single file called scriptfiles.sh from MyFiles.sh.tar use the following command.

# tar -xvf MyFiles.sh.tar scriptfiles.sh
# tar --extract --file=MyFiles.sh.tar scriptfiles.sh


9. Untar Single file from tar.gz File

To extract a single file tecmintbackup.xml from tecmintbackup.tar.gz archive file, use the command as follows.

# tar -zxvf tecmintbackup.tar.gz tecmintbackup.xml
# tar --extract --file=tecmintbackup.tar.gz tecmintbackup.xml


10. Untar Single file from tar.bz2 File

To extract a single file called index.php from the file Phpfiles-org.tar.bz2 use the following option.

# tar -jxvf Phpfiles-org.tar.bz2 home/tbc/index.php
# tar --extract --file=Phpfiles-org.tar.bz2 /home/tbc/index.php


Untar Multiple Files

11. Untar Multiple files from tar, tar.gz and tar.bz2 File

To extract or untar multiple files from the tar, tar.gz and tar.bz2 archive file. For example the below command will extract “file 1” “file 2” from the archive files.

# tar -xvf tbc-04-05-16.tar "file 1" "file 2" 

# tar -zxvf MyImages-04-05-16.tar.gz "file 1" "file 2" 

# tar -jxvf MyFiles.tar.bz2 "file 1" "file 2"


12. Extract Group of Files using Wildcard

To extract a group of files we use wildcard based extracting. For example, to extract a group of all files whose pattern ends with .php from a tar, tar.gz and tar.bz2 archive file.

# tar -xvf MyFiles.tar --wildcards '*.php'

# tar -zxvf MyFiles.tar.gz --wildcards '*.php'

# tar -jxvf MyFiles.tar.bz2 --wildcards '*.php'


List Archive Files

13. List Content of tar Archive File

To list the content of tar archive file, just run the following command with option t (list content). The below command will list the content of MyFiles.tar file.

# tar -tvf MyFiles.tar
  • t – List content of tar file.


14. List Content tar.gz Archive File

Use the following command to list the content of tar.gz file.

# tar -tvf MyFiles.tar.gz


15. List Content tar.bz2 Archive File

To list the content of tar.bz2 file, issue the following command.

# tar -tvf MyFiles.tar.bz2


16. Add Files or Directories to tar Archive File

To add files or directories to existing tar archived file we use the option r (append). For example we add file xyz.txt and directory php to existing MyFiles.tar archive file.

The tar command don’t have a option to add files or directories to a existing compressed tar.gz and tar.bz2 archive file.

# tar -rvf MyFiles.tar xyz.txt

# tar -rvf MyFiles.tar php/
  • r – Append file to tar archive.


17. How To Verify tar, tar.gz and tar.bz2 Archive File

To verfify any tar or compressed archived file we use option as W (verify). To do, just use the following examples of command. (Note : You cannot do verification on a compressed ( *.tar.gz, *.tar.bz2 ) archive file).

# tar tvfW MyFiles.tar
  • W – Verify tar archive.


18. Check the Size of the tar, tar.gz and tar.bz2 Archive File

To check the size of any tar, tar.gz and tar.bz2 archive file, use the following command. For example the below command will display the size of archvie file in Kilobytes (KB).

# tar -czf - MyFiles.tar | wc -c

# tar -czf - MyImages.tar.gz | wc -c

# tar -czf - MyVideos.tar.bz2 | wc -c


Tar Usage and Options

  • c – create a archive file.
  • x – extract a archive file.
  • v – show the progress of archive file.
  • f – filename of archive file.
  • t – viewing content of archive file.
  • j – filter archive through bzip2.
  • z – filter archive through gzip.
  • r – append or update files or directories to existing archive file.
  • W – Verify a archive file.
  • wildcards – Specify patters in unix tar command.


04. May 2016 by Alauddin Ansari Linux Leave a comment 3,192 views

Leave a Reply