Sunday, January 22, 2012

Learn tar command with examples in linux or unix

Tar command in unix:-

Tar command is abbreviated as tape archive.tar is one of the most popular zipped/achieve command in Unix like zipping in windows to compress the files and store all the files into single file tar.gz.

tar command examples in linux
tar and gzip are more popular zipped archive commands in Linux or Unix like zipping, WinRAR etc in windows. I have also blogged about Top 10 gzip examples in linux,unix  for gzip usage.

Most of the open source frameworks like Maven, Tomcat, JBoss etc..are shipped in tar.gz format to install in Unix environments.blogging all these for reference.


Tar example usage:-


1. Creating archive tar.gz for a folder or tar the files:-

Tar –cvzf   newfilename.tar.gz existing-folder-name

This will compress create the tar version/zipped as new-file-name.tar.gz from the existing folder. The structure of files, directories, subdirectories in existing-folder are same way created for new-file-name.tar.gz

c- creating new gz file
z - compressed the files
v- print the list of the files shown in stdout
f- with this option, archive name is specified within the command

2.Extract/unzip tar.gz file into a folder or untar
Tar –xvzf filename.tar.gz

X option means unzip the contents of this archive
Remaining options are same as like in #1

This will extract and copy all the files to the current directory with the folder as filename where this command is running.

3. Show all the files of the tar file

tar  tvf  filename.tar.gz

This will show all the files in filename.tar.gz to stdout console.
Option t – list all the contents of the archive

Advantages with tar are it will take a backup of your files
The disadvantage is a single file cannot be extracted from the zipped or archived file

Extract or unzip archive with gunzip, tar command:-


With this both commands, we can be used to unzip the tar.gz or .gz files.’


To unzip the tar.gz file, there are two steps involved. First, unzip .tar.gz into tar file in the following command and then use the tar command

gunzip filename.tar.gz
tar –xzf filename.tar 

filename.tar is created with the first command, this is passed to tar command extract the files in the tar.

Hope you got the basic start for tar, gunzip with examples.


Please feel free to comment and share your thoughts.

Related article


EmoticonEmoticon