Archiving and Compression

From Dikapedia
Jump to: navigation, search


It is always good to have a backup. You can use the cp command, or you can archive it using tar and compress it using gzip, that way you will save some space.


Archiving and Compression



This will archive and compress using gzip! Be sure to end in '.gz' and use -z, -z is for gzip:

tar -zcvf [newArchiveName.tar.gz] [file/dir you want to archive]

This will archive files and compressing using bzip2! Be sure to end name with '.bz2' and use -j. -j is for bzip:

tar -jcvf [newArchive.tar.bz2] [file/dir]

I.e: backed up your home directory and stored it into Backups directory, perserves permissions, verbose, gzip/compressions, etc.:

tar -cvpzf ~/Backups/backup.tar.gz ~/  

How I backed up my /etc/letsencrypt directory, did the same to my /var/www:

DATE=$(date +"%d-%b-%Y"); tar -zcvf ./letsencrypt-$DATE.tar.gz letsencrypt/
  • NOTE, when you extract the files it will create the file where you are unless you specify.


How to extract .tar.xz file

tar -xf linux-5.6.13.tar.xz
tar -xvf linux-5.6.13.tar.xz
tar -Jxvf linux-5.6.13.tar.xz
## extract tar.xz files aka .txz file ##
tar --xz -xf archive.txz

Archiving Files



tar - an archiving utility that DOES NOT do compression. Allows us to create archive. Collects a number of files/dir into a single file for backup/archiving.


To archive files, where c -> create ; v -> Verbose ; f -> file or archive device; * -> all files and directories:

tar -cvf  [newArchive.tar] [dir/file you want to archive]

To eXtract the contents from the archived file:

tar -xvf  [Archive.tar]

To show the contents of the archive folder (-t):

tar -tvf  [Archive.tar]

To add a file/dir to an archived file:

tar -rvf  [Archive.tar] [file/dir]

To perserve the permissions of the original file:

tar -p

To compress/uncompress the file size using gzip (Recommend you SHOULD do it, why not):

tar -z

Real world example from work: eXtract, compress(gzip), verbose, file

tar -xzvf [archive.tar.gz] [dir/file]


File Compression



gzip - compresses quicker (z)

Compress file:

gzip [file] [file]

Eextract/normalize file

gzip -d [file.gz] 

Provide a compression information (a compression/uncompression ratio):

gzip -l [file.gz] ...

To decompress:

gunzip -d [file.tar.gz]




bzip2 - bzip2, bunzip2 - a block-sorting file compressor. bzip2 compresses files using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. Bzip2 uses LESS SPACE for the compressed file (j).

Compresses file

bzip2 [file] 

Extract files from bzip

bunzip2 [file.bz2]