AWK Command

Tar Command in UNIX & Linux with example

Tar Command in UNIX & Linux with example

tar command in unix

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 and more.

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 Command in UNIX & Linux with example

In the above command:

c – create a new archive

v – verbosely list files which are processed.

f – following is the archive file name


Tar Command in UNIX : 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 Command in UNIX & Linux with example

z – filter the archive through gzip

Creating a bzipped tar archive using option cvjf. Create a bzip2 tar archive as shown below:

Tar Command in UNIX

j – filter the archive through bzip2


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 Command in UNIX

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.

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

Use the option j for uncompressing a bzip2 tar archive.

Note: In all the above examples v is optional, which is used to list the files being processed.


3. Tar Command in UNIX : 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.

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

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

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

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


4. 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.

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


5. 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 Command in UNIX

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 Command in UNIX

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

Tar Command in UNIX


6. 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 Command in UNIX

Options explanation:

–wildcards *.pl – files with pl extension


7. Tar Command in UNIX : 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 Command in UNIX

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

Tar Command in UNIX

Tar Command in UNIX


9. Tar Command in UNIX : 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 Command in UNIX

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 Command in UNIX


10.Tar Command in UNIX : Estimate the tar archive size

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

Tar Command in UNIX

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

Tar Command in UNIX

Read here : SED Command in UNIX & Linux with example

Leave a Reply