Monday, May 20, 2013


Tar command is specially designed to zip and extract the data. It helps to reduce the space while getting backed up data. It is used to store and extract files from an archive file. Let’s understand tar command with examples:

How to archive folder in tar extension by tar command

Tar command requires two arguments to archive the folder i.e. first argument is output file name with tar extension and second is source folder name which we want to archive.
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# tar -cvf logs.tar logs/
logs/
logs/catalina.2012-12-03.log
logs/localhost.2012-12-03.log
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28#

-c option with tar means – create a new archive file
-v option – verbose mode
-f option – forcefully create archive.
After this command you need to use gzip command to get much archived file as compared to tar as :
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs  logs.tar
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# gzip logs.tar
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs  logs.tar.gz
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28#

Compression ratio of this technique is very high. Generally this technique is used for backing up large data.

How to extract archive by tar command

Now extract the above archived file by tar and gunzip command. Extraction is very easy and you need to remember some options of tar only.
-x option with tar – extract the archive
And no need of any option with gunzip to extract gz file.

root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs  logs.tar.gz
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# gunzip logs.tar.gz
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs  logs.tar
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# rm -rf logs
##removing logs folder otherwise it will overwrite the existing folder
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs.tar
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# tar -xvf logs.tar
logs/
logs/catalina.2012-12-03.log
logs/localhost.2012-12-03.log
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs  logs.tar
##tar files remains as it as but new extracted folder will create

We can extract tar files or gz files directly by tar command as follows with –z option.
-z option – extract gz file
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs.tar.gz
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# tar -xvzf logs.tar.gz
logs/
logs/catalina.2012-12-03.log
logs/localhost.2012-12-03.log
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logslogs.tar.gz
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28#

How to update archive file by tar commandi.e. add new file in archive

We can update archive file by tar command. It is really very useful trick to update our backup data instead of creating new backup file.

root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs  logs.tar
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28#
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# touch test1
//New file created here
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# tar -uvf logs.tar test1
test1
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs  logs.tar  test1
## test1 is already here, so we will remove it to extract tar file and then we can understand archive is updated or not
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# rm–rf  logs test1
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
 logs.tar
##Now there is no logsor test1 file.
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# tar -xvf logs.tar
logs/
logs/catalina.2012-12-03.log
logs/localhost.2012-12-03.log
test1
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs  logs.tar  test1



Posted by Machindra Dharmadhikari On 5/20/2013 12:01:00 AM No comments

0 comments:

Post a Comment

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

    Chitika Ads 2

    Histat

    About

    Enter your email address:

    Delivered by FeedBurner