Monday, September 9, 2013

Most frequently used UNIX commands : tar command

This article provides practical examples for most frequently used commands in UNIX.

tar command : 

In Unix, the name of the tar command is short for tape archiving, the storing of entire file systems onto magnetic tape, However, a more common use for tar is to simply combine a few files into a single file, for easy storage and distribution.                                                                 

Creating an archive using tar command
                 
Create a new tar archive :                  
 $ tar cvf archive_name.tar dirname/
Extract from an existing tar archive:
$ tar xvf archive_name.tar
View an existing tar archive:
$ tar tvf archive_name.tar
In the above command:
  • c – create a new archive.
  • v – verbosely list files which are processed.
  • f – following is the archive file name.
  • x – extract files from archive.

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.

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.

Extracting (untar) an archive using tar command

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

Listing an archive using tar command

View the tar archive file content without extracting using option tvf                                              

$ tar tvf archive_name.tar

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

$ tar tvfz archive_name.tar.gz

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

$ tar tvfj archive_name.tar.bz2

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

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.
 $ 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/

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

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'
  • –wildcards *.pl – files with pl extension

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

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