Archive is just a bundle or file that contain files and directories. We could use this bundle to store it as one file and would easy to handle, store, move etc.
tar
In Unix/Linux system , tar is primary archiving utility. Even now a days it is also used in windows systems as well.
In this post we will try to see various possible options that could use while using tar utility. we will try to cover various examples for same.

1. Creating tar archive

For creating tar archive using tar command use below format. Archive could be made for file and directories.

Syntx for same

tar -cvf archive_file.tar file1 file2 file3 directories 
#tar -cvf etc.tar /etc f1 f2 f3
f1
f2
f3

In above command, option used.

  • c — create a new archive file
  • v — verbosely list files processed
  • f — following is the archive file name
  • In above command we have create an archive name etc.tar contain /etc and three file f1,f2 and f3. So that this bundle will contain all these files and directory.

    2. Creating zip tar archive

    There is possbility to compress archive file with zip utility, so tar have facility to compress in same command. we have use -z option for gzip.

    Syntx for same

    tar -czvf archive_file.tar file1 file2 file3 directories 
    
    #tar -czvf etc.tar.gz /etc f1 f2 f3
    
    We can see complete difference between zip tar zrchive and normal tar archive.
    
    #ls -lh etc.tar*
    -rw-r--r-- 1 root root  25M Feb 23 15:53 etc.tar
    -rw-r--r-- 1 root root 8.4M Feb 23 15:53 etc.tar.gz
    

    3. Creating bzip tar archive

    In linux there is one more zip utility which can compress more than zip.we have to use -j for bzip2.

    Syntx for same

    tar -cjvf archive_file.tar file1 file2 file3 directories 
    
    -rw-r--r-- 1 root root 25M Feb 23 15:53 etc.tar
    -rw-r--r-- 1 root root 8.4M Feb 23 15:53 etc.tar.gz
    -rw-r--r-- 1 root root 7.3M Feb 23 16:03 etc.tar.bz2
    

    4. Unarchive tar archive

    As we archive file and directories, we have to un-archive them as well. We have to use -x for extract archive files.

    Syntx for same

    tar -xvf archive_file.tar
    
    Unarchive tar file
    #tar -xvf etc.tar
    
    Unarchive tar.gz file
    #tar -xzvf etc.tar.gz
    
    Unarchive tar.bz2 file
    #tar -xjvf etc.tar.bz2
    

    4. List tar archive

    We can also list files inside bundle or archive, this would make easy to to find out files and directories inside archive.

    Syntx for same

    tar -tvf archive_file.tar
    
    List tar file
    #tar -tvf etc.tar
    
    List tar.gz file
    #tar -tzvf etc.tar.gz
    
    List tar.bz2 file
    #tar -tjvf etc.tar.bz2
    

    5. Extract selected files from tar

    Like unarchive complete tar file, there is possibilitiy to extract few selected files from tar file like below.

    Syntx for same

    tar -xvf archive_file.tar file1 file2 file3 directories 
    
    Unarchive tar file
    #tar -xvf etc.tar etc/hosts etc/fstab etc/passwd etc/group
    etc/hosts
    etc/passwd
    etc/fstab
    etc/group
    
    Unarchive tar.gz file
    #tar -xzvf etc.tar.gz etc/hosts etc/fstab etc/passwd etc/group
    etc/hosts
    etc/passwd
    etc/fstab
    etc/group
    
    Unarchive tar.bz2 file
    #tar -xjvf etc.tar.bz2 etc/hosts etc/fstab etc/passwd etc/group
    etc/hosts
    etc/passwd
    etc/fstab
    etc/group
    
    Use wildcards while Unarchive .tar for group of files
    #tar -xjvf etc.tar.bz2 etc/hosts etc/*/*.conf
    

    5. Append selected files in tar

    Sometime we need to add some more file in tar file, yes we could do this with -r option like in below way.

    Syntx for same

    tar -rvf archive_file.tar file1 file2 file3 directories 
    
    Append files or directories in tar file
    #tar -rvf etc.tar f1 f2 f3
    f1
    f2
    f3
    

    Note: We 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 -rzvf etc.tar.gz f1 f2 f3
    tar: Cannot update compressed archives
    Try `tar --help' or `tar --usage' for more information.
    

    6. Delete selected files in tar

    Sometime we need to delete files from tar archive, yes there is possibility for this, we could delete specfic files from tar archive.

    Syntx for same

    tar --delete -vf archive_file.tar file1 
    
    #tar -tvf etc.tar etc/hosts
    -rw-r--r-- root/root       195 2016-02-11 10:38 etc/hosts
    
    #tar --delete -vf  etc.tar etc/hosts
    
    #tar -tvf etc.tar etc/hosts
    tar: etc/hosts: Not found in archive
    tar: Exiting with failure status due to previous errors