Wednesday, June 21, 2017

Linux Tar Command Tutorial with Examples

This is "How To" guide to show us, how one can create/extract/compress/search archive file using the tar command in Linux. We would discuss below points regarding tar command:

What is the utility of tar command?
What are different types of compression technique used with tar command?
How to install tar package?
5 Tar Command Examples


What is the utility of tar command?

The Linux tar stands for tape archiver which is an archiving utility designed to create tar archives, compress the tar archives using different compression techniques, extract the contents of tar archives, list & search the content of tar archives and add files or directories to the existing tar archives.
  • Tar was originally developed for the purpose of backing up files to tape-based storage devices but now it is used for software distribution and for performing full and incremental backups of disks.
  • Tar archives can contain multiple files and directories without eradicating file system attributes like user & group permissions, access & modification dates, and directory structures.

What are different types of compression technique used with tar command?

GNU tar supports a wide variety of compression programs like gzip, bzip2, lzip, lzma, lzop, xz and traditional compress. Creating a compressed archive is very simple. We only need to specify a compression option along with the usual archive creation commands. Depending upon compression technique used, tar file can have different file format or extensions that are described below:
  • No Compression: Uncompressed archive file having .tar extension.
  • Gzip Compression: Compressed archive file having .tar.gz or .tgz extension.  It's most widely used compression program for tar.  The compression option is `-z` (`--gzip`) to create a gzip compressed archive.
  • Bzip2 Compression: Compressed archive file having .tar.bz2 extension. It offers a better compression then the Gzip format but it takes longer time to compress the archive file as compared to Gzip. The compression option is `-j` (`--bzip2`) to create a bzip2 compressed archive.
  • Lzip Compression: Compressed archive file having .lz extension. Lzip can compress about as fast as gzip (lzip -0), or compress most files more than bzip2 (lzip -9). Decompression speed is intermediate between gzip and bzip2. The compression option is `--lzip' to create an lzip compressed archive.
  • Lzop Compression: Compressed archive file having .lzo extension. It is the fastest compressor and decompressor around.
Among these, Gzip and Bzip2 are widely used. We should use Gzip for faster compression and Bzip2 where archive file size is concerned (smaller size as better compressed but slower).

How to install tar package?

The tar package is installed on most of the Linux systems by default. But if tar command is not found then one can execute below commands depending upon their Linux distribution to get it installed. The "sudo" privilege is required to run the commands with root privileges.

sudo yum install tar    # CentOS
sudo apt-get install tar # Ubuntu

5 Tar Command Examples

  1. Create/Compress tar Archive File 

     To create an archive of a directory and its contents, we need to run below command on the shell.
    tar -cvf archive.tar /path/to/directory      #archive but not compressed
    tar -cvf archive.tar /path/to/directory1 /path/to/filename1 
    tar -cvzf archive.tar.gz /path/to/directory  #compressed gzip archive file 
    tar -cvjf archive.tar.bz2 /path/to/directory #compressed bzip2 archive file 
    tar -cvzf archive.tar.gz /path/to/directory --exclude=/path/directory-or-file
    tar -cvzf archive.tar.gz /path/to/directory --exclude=*.mp4 
    
    Let's discuss the each options we have used in the above command for creating tar archive file.
    • [c] - stands for create, creates a new .tar archive file.
    • [v] - stands for verbose, show the .tar file progress
    • [f] - stands for file, specify the name of an archive file.
    • [z] - for Gzip compression.
    • [j] - for Bzip2 compression.
    • [--exclude] - for excluding directories or files, also accepts patterns.
  2. Extract the Content of tar Archive File 

    To extract a tar file, we need to run below command on the shell.
    #Extracts the contents of archive file in the current working directory
    tar -xvf archive.tar    
    tar -xvf archive.tar.gz    
    tar -xvf archive.tar.bz2   
    tar -xvzf archive.tar.gz   
    tar -xvjf archive.tar.bz2  
    
    #Extracts single/multiple files in current working directory
    tar -xvf archive.tar /path/to/file1 /path/to/file2
    tar -xvf archive.tar.gz /path/to/file1 /path/to/file2
    tar -xvzf archive.tar.gz /path/to/file1 
    tar -xvjf archive.tar.bz2 /path/to/file1 /path/to/file2
    
    #Extract the contents of archive file in the specified directory
    tar -xvzf archive.tar.gz -C /path/to/directory
    
    #Extracts multiple files using wildcards 
    tar -xvf archive.tar --wildcards '*.java'
    tar -zxvf archive.tar.gz --wildcards '*.java'
    tar -jxvf archive.tar.bz2 --wildcards '*.java'
    
    Note: Using [xvf] option, one can extract the content of archive file whether it is .tar, .tar.gz or .tar.bz2. But it's better to use the [z] or [j] options while extracting from .tar.gz & .tar.bz2 respectively. 

    Let's discuss the new options that we have used in the above command for extracting tar archive file.
    • [x] - stands for extract, extract one or more members from an archive file.
    • [C] - extract the contents of the archive file to a specific directory.
    • [--wildcards] - extracts multiple files based on specified pattern from the archive file.
  3. List the content of tar Archive File

    The command used for listing the content of tar archive file would greatly help in three scenarios:
    • One would like to know what's inside the tar file or
    • To get exact path of the particular file in the archive so that one can extract particular file from the archive
    • Search the archive file whether it contains particular file or not
    To list the content of tar archive file, we need to run below command on the shell. See the new [t] option which is used to list the contents of an archive file. 
    tar -tvf archive.tar 
    tar -tvf archive.tar.gz 
    tar -tvf archive.tar.bz2
    tar -tvf archive.tar.gz | grep test.txt 
  4. Add Files or Directories to tar(.tar ony) Archive File

    To add files or directories to an existing tar archive file (.tar only), we need to run below command on the shell. See the new [r] option which is used to append files to the end of the archive.
    tar -rvf archive.tar /path/to/directory-or-file
    
    Note:The tar command don’t have any option to add files or directories to an existing compressed tar.gz and tar.bz2 archive file. If we try then we would get error like "tar: Cannot update compressed archives".

    If one would like to add files or directories to an existing .tar.gz or .tar.bz2 file then there is a workaround which is described below:
    #For .tar.gz file
    gunzip archive.tar.gz   #decompress 
    tar -rf archive.tar /path/to/directory-or-file #add files now 
    gzip archive.tar #again compress 
    
    #For .tar.bz2 file
    bunzip2 archive.tar.bz2  #decompress 
    tar -rf archive.tar /path/to/directory-or-file #add files now 
    bzip2 archive.tar #again compress 
    
  5. Estimate the tar Archive Size

    One can estimate the size of .tar, .tar.gz and .tar.bz2 files before creating it. To estimate tar archive size (in KB), we need to run below command on the shell.
    #For .tar file
    tar -cf - /directory/to/archive/ | wc -c
    
    #For .tar.gz file
    tar -czf - /directory/to/archive/ | wc -c
    
    #For .tar.bz2 file
     tar -cjf - /directory/to/archive/ | wc -c
    
The tar command includes a large number of additional options which are not described here. For more information, one can run the info tar command to see it's detailed information page (press the q key to quit the information page). One can also read tar’s manual page online from here.

Thank you for reading this article. Hope you would like it. If you have any suggestion, question or if we've missed any example do let us know in the comments and please don’t forget to share this article with your friends. This is the best way to say thanks. Happy Learning!!

No comments:

Post a Comment