Understanding tar(Tape Archive) command in Linux

tar(Tape Archive) is used to group multiple files in to a single archive file.An Archive file it consists of any number of different files and also it contains the details to allow them to restored to their original format.Archive files are more easier to transmit or upload to the destination drive or media.

tar program first originally developed for the backups on the magnetic tape drive,later it can be now used to create archive files on a file system.tar will not compress the size of the data ‘s ,but it is very easy to compress the archive files created with tar utility.

Basic syntax for “tar”:

#tar   <options>     <archive file>    <files to be archive>

Let us  see the available options  that can be used with the tar command

-c = To create a new archive file(with the extension .tar)

-v = Verbose Output

-f = To mention the files

-x = To untar the archive file

-t = List the contents from the archive file
Ex:1 To create tar archive file

In this example i will add different files from various location in to the archive file

/backup.tar = Archive file name

/mylog = A file from / directory

/mydir/database = A file from the /mydir path

/var/userdata = A file  from /var directory path
#tar  -cvf   /backup.tar  mydata.tar  /mylog    /mydir/database   /var/userdata

#ls

As you can see from the above output ,the archived file with the name backup.tar is created successfully.

Ex:2 Untar the archive file
##If you want to unarchive in your current path run the below syntax

#tar -xvf  mydata.tar

##If you want to unarchive in different path run the below syntax

#tar -xvf  mydata.tar   -C   <destination path>

In this example i use the different destination path to untar

#tar  -xvf   mydata.tar    -C   /home/vasanth

Here i use the destination path  as /home/vasanth directory

Now go and check the destination path whether it is successfully unextracted the files

#cd   /home/vasanth

#ls

As you can see from the above output ,all the files from the archive successfully unarchived

Ex:3 To List the contents from the Archive file

To display the contents from the archive file ,apply -l option with the tar command as shown below

#tar   -tvf   <archive file>

#tar   -tvf   /backup.tar

The above command will display the contents of the /backup.tar file

The above command will not unarchive the archive file,it just list the content from the archive file.

Ex:4 To add additional file inside the archive file

If you want to add any additional files inside the  archive file apply -t option with the tar command as follows,

#touch  /rootlog

For More tutorial videos  subscribe my YouTube channel  Linux Vasanth

Now i will add /rootlog inside /backup.tar file

#tar   -rvf  backup.tar  rootlog
-r  = To add additional files in to the archive file

Check whether it is added or not

#tar  -tvf  /mybackup.tar

As we can see from the above  output,the file /rootlog is added inside the archive file

Ex:5 To delete a specified file from the archive file

In this example i will delete the file rootlog from the backup.tar file

#tar  -f   backup.tar   --delete   rootlog

Note: To delete multiple files ,add space and give the file names

To confirm whether it is removed from the archive file

#tar  -tvf  /backup.tar

As you can see from the above output rootlog file has been removed from the archive file.

I hope you have enjoyed this tutorial if so Kindly subscribe and share it with your friends.
                                          🙏🙏 Thank you 🙏🙏
You can reach me  at  vasanth@linuxvasanth.com
#############################################################
For More Videos Subscribe My Youtube Channel  Linux Vasanth
If you found this article useful, Kindly Subscribe here 👉  Click this link to Subscribe

################################################################################