Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

6/4/2015

TheUltimateTarCommandTutorialwith10PracticalExamples

FacebookAccountSignUp
ConnectWithYourFriendsOnline.JointheFacebookCommunityFree!

Menu
Home
FreeeBook
StartHere
Contact
About

TheUltimateTarCommandTutorialwith10
PracticalExamples
bySathiyaMoorthyonApril26,2010
49

Like

73

Tweet

33

OnUnixplatform,tarcommandistheprimaryarchivingutility.
Understandingvarioustarcommandoptionswillhelpyoumasterthearchivefilemanipulation.
Inthisarticle,letusreviewvarioustarexamplesincludinghowtocreatetararchives(withgzipandbzip
compression),extractasinglefileordirectory,viewtararchivecontents,validatetheintegrityoftararchives,
findingoutthedifferencebetweentararchiveandfilesystem,estimatethesizeofthetararchivesbefore
creatingitetc.,

1.Creatinganarchiveusingtarcommand
http://www.thegeekstuff.com/2010/04/unixtarcommandexamples/

1/18

6/4/2015

TheUltimateTarCommandTutorialwith10PracticalExamples

Creatinganuncompressedtararchiveusingoptioncvf
Thisisthebasiccommandtocreateatararchive.
$tarcvfarchive_name.tardirname/

Intheabovecommand:
ccreateanewarchive
vverboselylistfileswhichareprocessed.
ffollowingisthearchivefilename
Creatingatargzippedarchiveusingoptioncvzf
Theabovetarcvfoption,doesnotprovideanycompression.Touseagzipcompressiononthetararchive,use
thezoptionasshownbelow.
$tarcvzfarchive_name.tar.gzdirname/

zfilterthearchivethroughgzip
Note:.tgzissameas.tar.gz
Note:Iliketokeepthecvf(ortvf,orxvf)optionunchangedforallarchivecreation(orview,orextract)and
addadditionaloptionattheend,whichiseasiertoremember.i.ecvfforarchivecreation,cvfzforcompressed
gziparchivecreation,cvfjforcompressedbzip2archivecreationetc.,Forthismethodtoworkproperly,dont
giveinfrontoftheoptions.

Creatingabzippedtararchiveusingoptioncvjf
Createabzip2tararchiveasshownbelow:
$tarcvfjarchive_name.tar.bz2dirname/

jfilterthearchivethroughbzip2
gzipvsbzip2:bzip2takesmoretimetocompressanddecompressthangzip.bzip2archivalsizeislessthan
gzip.
http://www.thegeekstuff.com/2010/04/unixtarcommandexamples/

2/18

6/4/2015

TheUltimateTarCommandTutorialwith10PracticalExamples

Note:.tbzand.tb2issameas.tar.bz2

2.Extracting(untar)anarchiveusingtarcommand
Extracta*.tarfileusingoptionxvf
Extractatarfileusingoptionxasshownbelow:
$tarxvfarchive_name.tar

xextractfilesfromarchive
Extractagzippedtararchive(*.tar.gz)usingoptionxvzf
Usetheoptionzforuncompressingagziptararchive.
$tarxvfzarchive_name.tar.gz

Extractingabzippedtararchive(*.tar.bz2)usingoptionxvjf
Usetheoptionjforuncompressingabzip2tararchive.
$tarxvfjarchive_name.tar.bz2

Note:Inalltheabovecommandsvisoptional,whichliststhefilebeingprocessed.

3.Listinganarchiveusingtarcommand
Viewthetararchivefilecontentwithoutextractingusingoptiontvf
Youcanviewthe*.tarfilecontentbeforeextractingasshownbelow.
$tartvfarchive_name.tar

Viewthe*.tar.gzfilecontentwithoutextractingusingoptiontvzf
Youcanviewthe*.tar.gzfilecontentbeforeextractingasshownbelow.
$tartvfzarchive_name.tar.gz

Viewthe*.tar.bz2filecontentwithoutextractingusingoptiontvjf
Youcanviewthe*.tar.bz2filecontentbeforeextractingasshownbelow.
$tartvfjarchive_name.tar.bz2

4.Listingoutthetarfilecontentwithlesscommand
Whenthenumberoffilesinanarchiveismore,youmaypipetheoutputoftartoless.But,youcanalsouse
lesscommanddirectlytoviewthetararchiveoutput,asexplainedinoneofourpreviousarticleOpen&View
10DifferentFileTypeswithLinuxLessCommandTheUltimatePowerofLess.

http://www.thegeekstuff.com/2010/04/unixtarcommandexamples/

3/18

6/4/2015

TheUltimateTarCommandTutorialwith10PracticalExamples

5.Extractasinglefilefromtar,tar.gz,tar.bz2file
Toextractaspecificfilefromatararchive,specifythefilenameattheendofthetarxvfcommandasshown
below.Thefollowingcommandextractsonlyaspecificfilefromalargetarfile.
$tarxvfarchive_file.tar/path/to/file

Usetherelevantoptionzorjaccordingtothecompressionmethodgziporbzip2respectivelyasshownbelow.
$tarxvfzarchive_file.tar.gz/path/to/file
$tarxvfjarchive_file.tar.bz2/path/to/file

6.Extractasingledirectoryfromtar,tar.gz,tar.bz2file
Toextractasingledirectory(alongwithitssubdirectoryandfiles)fromatararchive,specifythedirectory
nameattheendofthetarxvfcommandasshownbelow.Thefollowingextractsonlyaspecificdirectoryfroma
largetarfile.
$tarxvfarchive_file.tar/path/to/dir/

Toextractmultipledirectoriesfromatararchive,specifythoseindividualdirectorynamesattheendofthetar
xvfcommandasshownbelow.
$tarxvfarchive_file.tar/path/to/dir1//path/to/dir2/

Usetherelevantoptionzorjaccordingtothecompressionmethodgziporbzip2respectivelyasshownbelow.
$tarxvfzarchive_file.tar.gz/path/to/dir/
$tarxvfjarchive_file.tar.bz2/path/to/dir/

7.Extractgroupoffilesfromtar,tar.gz,tar.bz2archivesusingregularexpression
Youcanspecifyaregex,toextractfilesmatchingaspecifiedpattern.Forexample,followingtarcommand
extractsallthefileswithplextension.
$tarxvfarchive_file.tarwildcards'*.pl'

Optionsexplanation:
wildcards*.plfileswithplextension

8.Addingafileordirectorytoanexistingarchiveusingoptionr
Youcanaddadditionalfilestoanexistingtararchiveasshownbelow.Forexample,toappendafileto*.tarfile
dothefollowing:
$tarrvfarchive_name.tarnewfile

Thisnewfilewillbeaddedtotheexistingarchive_name.tar.Addingadirectorytothetarisalsosimilar,
$tarrvfarchive_name.tarnewdir/

Note:Youcannotaddfileordirectorytoacompressedarchive.Ifyoutrytodoso,youwillgettar:Cannot
updatecompressedarchiveserrorasshownbelow.
http://www.thegeekstuff.com/2010/04/unixtarcommandexamples/

4/18

6/4/2015

TheUltimateTarCommandTutorialwith10PracticalExamples

$tarrvfzarchive_name.tgznewfile
tar:Cannotupdatecompressedarchives
Try`tarhelp'or`tarusage'formoreinformation.

9.VerifyfilesavailableintarusingoptionW
Aspartofcreatingatarfile,youcanverifythearchivefilethatgotcreatedusingtheoptionWasshownbelow.
$tarcvfWfile_name.tardir/

Ifyouareplanningtoremoveadirectory/filefromanarchivefileorfromthefilesystem,youmightwantto
verifythearchivefilebeforedoingitasshownbelow.
$tartvfWfile_name.tar
Verify1/file1
1/file1:Modtimediffers
1/file1:Sizediffers
Verify1/file2
Verify1/file3

IfanoutputlinestartswithVerify,andthereisnodifferslinethenthefile/directoryisOk.Ifnot,youshould
investigatetheissue.
Note:foracompressedarchivefile(*.tar.gz,*.tar.bz2)youcannotdotheverification.
Findingthedifferencebetweenanarchiveandfilesystemcanbedoneevenforacompressedarchive.Italso
showsthesameoutputasaboveexcludingthelineswithVerify.
Findingthedifferencebetweengziparchivefileandfilesystem
$tardfzfile_name.tgz

Findingthedifferencebetweenbzip2archivefileandfilesystem
$tardfjfile_name.tar.bz2

10.Estimatethetararchivesize
Thefollowingcommand,estimatesthetarfilesize(inKB)beforeyoucreatethetarfile.
$tarcf/directory/to/archive/|wcc
20480

Thefollowingcommand,estimatesthecompressedtarfilesize(inKB)beforeyoucreatethetar.gz,tar.bz2
files.
$tarczf/directory/to/archive/|wcc
508
$tarcjf/directory/to/archive/|wcc
428
49

Tweet

33

Like

73

>Addyourcomment

Ifyouenjoyedthisarticle,youmightalsolike..

http://www.thegeekstuff.com/2010/04/unixtarcommandexamples/

5/18

You might also like