The Ultimate Tar Command Tutorial with 10 Practical Examples

by SathiyaMoorthy on April 26, 2010

On Unix platform, tar command is the primary archiving utility. Understanding various tar command options will help you master the archive file manipulation.

In this article, let us review various tar examples including how to create tar archives (with gzip and bzip compression), extract a single file or directory, view tar archive contents, validate the integrity of tar archives, finding out the difference between tar archive and file system, estimate the size of the tar archives before creating it etc.,

1. Creating an archive using tar command

Creating an uncompressed tar archive using option cvf

This is the basic command to create a tar archive.

$ tar cvf archive_name.tar dirname/

In the above command:

  • c – create a new archive
  • v – verbosely list files which are processed.
  • f – following is the archive file name

Creating a tar gzipped archive using option cvzf

The above tar cvf option, does not provide any compression. To use a gzip compression on the tar archive, use the z option as shown below.

$ tar cvzf archive_name.tar.gz dirname/
  • z – filter the archive through gzip

Note: .tgz is same as .tar.gz

Note: I like to keep the ‘cvf’ (or tvf, or xvf) option unchanged for all archive creation (or view, or extract) and add additional option at the end, which is easier to remember. i.e cvf for archive creation, cvfz for compressed gzip archive creation, cvfj for compressed bzip2 archive creation etc., For this method to work properly, don’t give – in front of the options.

Creating a bzipped tar archive using option cvjf

Create a bzip2 tar archive as shown below:

$ tar cvfj archive_name.tar.bz2 dirname/
  • j – filter the archive through bzip2

gzip vs bzip2: bzip2 takes more time to compress and decompress than gzip. bzip2 archival size is less than gzip.

Note: .tbz and .tb2 is same as .tar.bz2

2. Extracting (untar) an archive using tar command

Extract a *.tar file using option xvf

Extract a tar file using option x as shown below:

$ tar xvf archive_name.tar
  • x – extract files from archive

Extract a gzipped tar archive ( *.tar.gz ) using option xvzf

Use the option z for uncompressing a gzip tar archive.

$ tar xvfz archive_name.tar.gz

Extracting a bzipped tar archive ( *.tar.bz2 ) using option xvjf

Use the option j for uncompressing a bzip2 tar archive.

$ tar xvfj archive_name.tar.bz2

Note: In all the above commands v is optional, which lists the file being processed.

3. Listing an archive using tar command

View the tar archive file content without extracting using option tvf

You can view the *.tar file content before extracting as shown below.

$ tar tvf archive_name.tar

View the *.tar.gz file content without extracting using option tvzf

You can view the *.tar.gz file content before extracting as shown below.

$ tar tvfz archive_name.tar.gz

View the *.tar.bz2 file content without extracting using option tvjf

You can view the *.tar.bz2 file content before extracting as shown below.

$ tar tvfj archive_name.tar.bz2

4. Listing out the tar file content with less command

When the number of files in an archive is more, you may pipe the output of tar to less. But, you can also use less command directly to view the tar archive output, as explained in one of our previous article Open & View 10 Different File Types with Linux Less Command — The Ultimate Power of Less.

5. Extract a single file from tar, tar.gz, tar.bz2 file

To extract a specific file from a tar archive, specify the file name at the end of the tar xvf command as shown below. The following command extracts only a specific file from a large tar file.

$ tar xvf archive_file.tar /path/to/file

Use the relevant option z or j according to the compression method gzip or bzip2 respectively as shown below.

$ tar xvfz archive_file.tar.gz /path/to/file

$ tar xvfj archive_file.tar.bz2 /path/to/file

6. Extract a single directory from tar, tar.gz, tar.bz2 file

To extract a single directory (along with it’s subdirectory and files) from a tar archive, specify the directory name at the end of the tar xvf command as shown below. The following extracts only a specific directory from a large tar file.

$ tar xvf archive_file.tar /path/to/dir/

To extract multiple directories from a tar archive, specify those individual directory names at the end of the tar xvf command as shown below.

$ tar xvf archive_file.tar /path/to/dir1/ /path/to/dir2/

Use the relevant option z or j according to the compression method gzip or bzip2 respectively as shown below.

$ tar xvfz archive_file.tar.gz /path/to/dir/

$ tar xvfj archive_file.tar.bz2 /path/to/dir/

7. Extract group of files from tar, tar.gz, tar.bz2 archives using regular expression

You can specify a regex, to extract files matching a specified pattern. For example, following tar command extracts all the files with pl extension.

$ tar xvf archive_file.tar --wildcards '*.pl'

Options explanation:

  • –wildcards *.pl – files with pl extension

8. Adding a file or directory to an existing archive using option -r

You can add additional files to an existing tar archive as shown below. For example, to append a file to *.tar file do the following:

$ tar rvf archive_name.tar newfile

This newfile will be added to the existing archive_name.tar. Adding a directory to the tar is also similar,

$ tar rvf archive_name.tar newdir/

Note: You cannot add file or directory to a compressed archive. If you try to do so, you will get “tar: Cannot update compressed archives” error as shown below.

$ tar rvfz archive_name.tgz newfile
tar: Cannot update compressed archives
Try `tar --help' or `tar --usage' for more information.

9. Verify files available in tar using option -W

As part of creating a tar file, you can verify the archive file that got created using the option W as shown below.

$ tar cvfW file_name.tar dir/

If you are planning to remove a directory/file from an archive file or from the file system, you might want to verify the archive file before doing it as shown below.

$ tar tvfW file_name.tar
Verify 1/file1
1/file1: Mod time differs
1/file1: Size differs
Verify 1/file2
Verify 1/file3

If an output line starts with Verify, and there is no differs line then the file/directory is Ok. If not, you should investigate the issue.

Note: for a compressed archive file ( *.tar.gz, *.tar.bz2 ) you cannot do the verification.

Finding the difference between an archive and file system can be done even for a compressed archive. It also shows the same output as above excluding the lines with Verify.

Finding the difference between gzip archive file and file system

$ tar dfz file_name.tgz

Finding the difference between bzip2 archive file and file system

$ tar dfj file_name.tar.bz2

10. Estimate the tar archive size

The following command, estimates the tar file size ( in KB ) before you create the tar file.

$ tar -cf - /directory/to/archive/ | wc -c
20480

The following command, estimates the compressed tar file size ( in KB ) before you create the tar.gz, tar.bz2 files.

$ tar -czf - /directory/to/archive/ | wc -c
508

$ tar -cjf - /directory/to/archive/ | wc -c
428
Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

If you enjoyed this article, you might also like..

  1. HowTo: The Ultimate Logrotate Command Tutorial with 10 Examples
  2. Linux cpio Examples: How to Create and Extract cpio Archives (and tar archives)
  3. Open & View 10 Different File Types with Linux Less Command – The Ultimate Power of Less
  4. The Power of Z Commands – Zcat, Zless, Zgrep, Zdiff Examples
  5. The Ultimate Bash Array Tutorial with 15 Examples
  

Vim 101 Hacks Book

{ 14 comments… read them below or add one }

1 krefik April 27, 2010 at 3:30 am

You missed few really useful examples, like ie.
tar cjf archive-$(date +%Y%m%d).tbz2 dir/
tar cjf archive-$(date +%Y%m%d).tbz2 dir/ -N ./archive-$(date -d yesterday +%Y%m%d)
for daily backups.

2 Ajeet S Raina April 27, 2010 at 4:08 am

I want to add up an important command too which most of us do face regularly:

Writing an archive in a different directory

You may also want to write an archive lto a different directory, like this:

tar czvf /tmp/mydirectory.tgz .

3 tej parkash April 27, 2010 at 7:36 am

tej@tej-desktop:~$ tar -czf – Music/ | wc -c
1358
tej@tej-desktop:~$ tar -cjf – Music/ | wc -c
1471

Why it is so. file tarred with j should be less in size????

4 Joe Black April 27, 2010 at 8:29 am

Tar command always seemed confusing to me. Your writeup clears any mis understanding. Nice work.

5 effe April 27, 2010 at 8:37 am

dear Ramesh,

first of all i’d like to thank you very much for your excellent work. being a newbie to BSD/linux thegeekstuff is my #1 choice to have a look at when it comes to help, understanding, and how-to… =:-) thank you very much again.

but right now, i am a bit confused: in 1. Creating an archvie using tar command, Note #2, you wrote that you like to keep the ‘cvf’ option unchanged for easy remembering, and you prefer to add additional options at the end. but if so, shouldn’t it be ‘cvfz’ for compressed gzip’s, and ‘cvfj’ for compressed bzip2′s? i mean to have ‘cvf’ together as you wrote one sentence before? did i miss something? or is it a simple spelling mistake?

again, sorry if it’s a stupid question, but keep in mind i’m still very new to tar (and all the other non-MS stuff), let’s call it doubtful, uncertain and precarious.

6 Karl O. Pinc April 27, 2010 at 9:57 am

No tar tutorial is complete without an example that does a cross-network copy.

This example uses GNU tar, with –numeric-owner and so can, as in this example, be used to copy an entire system onto a system with a blank disk drive that’s been put into another computer. The target system needs to have the new blank disk partitioned (fdisk, cfdisk, gparted, etc.), and filesystems created (mke2fs, etc.) and mounted (mount) in the appropriate places under /mnt/. It may be handy to boot the target system using a “live” Linux cd, usb key, etc.

If you are logged in, as root, on the target system, the one with the new drive, you use:

ssh sourcesystem.example.com ‘tar -C / –numeric-owner -czf – .’ | tar -C /mnt -xzpf -

If you are logged in, as root, on the source system, the one being copied, you use:

tar -C / –numeric-owner -czf – . | ssh targetsystem.example.com ‘tar -C /mnt -xzpf -’

Note that the above command, or any other sort of whole system backup and restore, should _not_ be used to “clone” a system because of the many little bits of supposed-to-be-unique data that a modern system contains. Use your package management system to “clone”.

7 mario April 27, 2010 at 10:27 am

Tar is common. But tar doesn’t generate fully POSIX-compliant archives. A better tool is “pax”. It’s also more reliable in reconstructing file access rights. (And it handles both, tar and cpio formats.)

8 loeppel April 27, 2010 at 10:44 am

@tej parkash: The compressed size depends on the content of your Folder.

9 Amit Agarwal April 27, 2010 at 7:54 pm

with latest version of tar, the options z or j does not need to be specified for listing or extracting.

10 Roger Roelofs April 27, 2010 at 11:17 pm

Thanks for the useful article. The one suggestion I’d make is that the code to estimate final archive file size isn’t very useful in real life. The example for estimating the final size of the archive actually creates the archive, (in memory at least) so the estimate will take almost as long as actually doing the command in the first place. For small file sets you really don’t care about the size of the final output. For larger file sets where you are likely to care, the time to get the estimate is prohibitive. However that example is a good demonstration of the power of combining command line utilities by piping data between them. Great job!

11 matus May 7, 2010 at 7:26 am

Nice explanation.

12 jose May 7, 2010 at 12:44 pm

tar does not use regular expressions (or regexp). it uses globbing – please do not mix these terms

13 David May 15, 2010 at 3:28 pm

Thanks – I’m always forgetting how to run tar to create a .tar.gz

14 djraj May 28, 2010 at 1:25 am

Really cool Tutorial.

Leave a Comment

Previous post:

Next post: