Creatingacompressedarchiveissimple:youjustspecifyacompressionoptionalongwiththeusualarchivecreationcommands.Availablecompressionoptionsaresummarizedinthetablebelow:
Forexample:
$tarczfarchive.tar.gz.YoucanalsoletGNUtarselectthecompressionprogrambasedonthesuffixofthearchivefilename.Thisisdoneusing‘--auto-compress’(‘-a’)commandlineoption.Forexample,thefollowinginvocationwillusebzip2forcompression:
$tarcafarchive.tar.bz2.whereasthefollowingonewilluselzma:
Readingcompressedarchiveisevensimpler:youdon’tneedtospecifyanyadditionaloptionsasGNUtarrecognizesitsformatautomatically.Thus,thefollowingcommandswilllistandextractthearchivecreatedinpreviousexample:
Theonlycasewhenyouhavetospecifyadecompressionoptionwhilereadingthearchiveiswhenreadingfromapipeorfromatapedrivethatdoesnotsupportrandomaccess.However,inthiscaseGNUtarwillindicatewhichoptionyoushoulduse.Forexample:
$catarchive.tar.gz|tartf-tar:Archiveiscompressed.Use-zoptiontar:Errorisnotrecoverable:exitingnowIfyouseesuchdiagnostics,justaddthesuggestedoptiontotheinvocationofGNUtar:
$catarchive.tar.gz|tartzf-Noticealso,thatthereareseveralrestrictionsonoperationsoncompressedarchives.Firstofall,compressedarchivescannotbemodified,i.e.,youcannotupdate(‘--update’,alias‘-u’)themordelete(‘--delete’)membersfromthemoradd(‘--append’,alias‘-r’)memberstothem.Likewise,youcannotappendanothertararchivetoacompressedarchiveusing‘--concatenate’(‘-A’).Secondly,multi-volumearchivescannotbecompressed.
Thefollowingoptionsallowtoselectaparticularcompressorprogram:
Filterthearchivethroughgzip.
Filterthearchivethroughxz.
Filterthearchivethroughbzip2.
Filterthearchivethroughlzip.
Filterthearchivethroughlzma.
Filterthearchivethroughlzop.
Filterthearchivethroughzstd.
Filterthearchivethroughcompress.
Theoutputproducedbytar--helpshowstheactualcompressornamesalongwitheachoftheseoptions.
Youcanuseanyoftheseoptionsonphysicaldevices(tapedrives,etc.)andremotefilesaswellasonnormalfiles;datatoorfromsuchdevicesorremotefilesisreblockedbyanothercopyofthetarprogramtoenforcethespecified(ordefault)recordsize.Thedefaultcompressionparametersareused.Youcanoverridethembyusingthe‘-I’option(seebelow),e.g.:
$tar-cfarchive.tar.gz-I'gzip-9-n'subdirAmoretraditionalwaytodothisistouseapipe:
$tarcf-subdir|gzip-9-n>archive.tar.gzCompressedarchivesareeasilycorrupted,becausecompressedfileshavelittleredundancy.Theadaptivenatureofthecompressionschememeansthatthecompressiontablesareimplicitlyspreadalloverthearchive.Ifyouloseafewblocks,thedynamicconstructionofthecompressiontablesbecomesunsynchronized,andthereislittlechancethatyoucouldrecoverlaterinthearchive.
Othercompressionoptionsprovidebettercontrolovercreatingcompressedarchives.Theseare:
Selectacompressionprogramtousebythearchivefilenamesuffix.Thefollowingsuffixesarerecognized:
Thecommandshouldfollowtwoconventions:
First,wheninvokedwithoutadditionaloptions,itshouldreaddatafromstandardinput,compressitandoutputitonstandardoutput.
Secondly,ifinvokedwiththeadditional‘-d’option,itshoulddoexactlytheopposite,i.e.,readthecompresseddatafromthestandardinputandproduceuncompresseddataonthestandardoutput.
Thelatterrequirementmeansthatyoumustnotusethe‘-d’optionasapartofthecommanditself.
#!/bin/shcase$1in-d)gpg--decrypt-|gzip-d-c;;'')gzip-c|gpg-s;;*)echo"Unknownoption$1">&2;exit1;;esacSupposeyounameit‘gpgz’andsaveitsomewhereinyourPATH.Thenthefollowingcommandwillcreateacompressedarchivesignedwithyourprivatekey: