Information TechnologyLinux

Linux: tar cheat sheet

Quick Brief:

Tar is tool to compress and decompress files, similar to the windows built in “ZIP” function where you can “send” a folder or file to a single file which can be decompressed on another machine and expand the folder as it were, tar is the in-built Linux equivalent .

Benefits of compression:
  • files take up less space once compressed
  • no need to transfer {x} amount of files when they’ve all been compressed into one
  • backup solutions are easier to manage if you just have one file with the date it was backed up
  • file transfers are a lot more reliable if you only have to transfer one file rather than {x} and have a number of them fail

Faster compression with tar.gz

Without a doubt gunzip (gzip) – .gz is way faster than other compression methods however with the speed you also miss out (or gain more rather) on size than you would with other methods – i.e. bzip2

Create our archive

~# tar -czf archive.tar.gz DirectoryOrFile

Extract our archive

~# tar -xzf tarfile.tar.gz

The important flags:

-c – Create an archive (tar.gz by default)

-x – Extract an archive

-f – Specify a filename (if specified, this needs to be at the end of a command)

-j – Specify bzip2 as the compression method

-v – Adds verbose output

Effective compression with bzip2

bzip archives are on average a lot better at compressing files than gunzip’s but take on average a lot longer, 2 or 3 times in some cases. Please note that if we are compressing a directory we will need to state the path followed by * as this will not be picked up by default.

Create our archive

~# tar -cjf archive.tar.gz DirectoryOrFile/*

Extract our archive

~# tar -xjf tarfile.tar.gz