Guide to Tar command

By | Ottobre 7, 2016

The command Tar (Tape Archive) is used to create file archives. It is often used with the combination of compression programs such as gzip and bzip2 to reduce the size of archvi and occupy less space. In this guide we will look at the main options to use Tar and a small tutorial on using to back up your data format tar.gz.

Let’s see how to use it…

1st Example

To create a simple archive without compression, the command to use is:

$ tar cvf archive_name.tar file1 fil2 file3 ...

To extract the archive using the command:

$ tar xvf archive_name.tar

2nd Example

Let’s create an archive containing the files within the directory /home/john

$ tar cvf home_john.tar /home/john/*

In this way we have created the archive home_john.tar with all the files in the home (/home/john/*)

And ‘possible to exclude certain directories, such as /home/john/downloads with the command:

$ tar cvf home_john.tar /home/john/* --exclude "/home/john/downloads"

3rd Example

We create a tar archive with gzip compressing it later. This is possible through 3 ways:

Using two passes with two lines:

$ tar cvf archive.tar file1
$ gzip archive.tar

It will create a file archive.tar.gz

Using two passes with a single line:

$ tar cvf archive.tar file1 && gzip archive.tar

It will create exactly the same file mentioned above.

Using a step and a line:

$ tar cvfz archive.tar.gz file1

To extract the archive .tar.gz you can use the command:

$ tar xvfz archivio.tar.gz

or in two steps:

$ gunzip archive.tar.gz
$ tar xvf archive.tar

Two passes the same line:

$ gunzip -c archive.tar.gz | tar -xf -

4th Example

Compress the archive using bzip2:

$ tar cjvf archive.tar.bz2 file1 file2

To extract the file you just created:

$ tar xjvf archive.tar.bz2

5th Example

To view the files in an archive .tar command to use is:

$ tar tf archive.tar

while for the archives .tar.gz the command to use is:

$ tar tgf archive.tar.gz

Backup the entire system

As we all know, having a backup of your entire system, sensitive data and some configuration files can sometimes be really comfortable. Let’s see how to do that using tar and gzip compression.

To back up the entire system, excluding the unnecessary directories (using the –exclude seen above), open a terminal as user “root” and run the command:

$ tar cvfzp /home/$USER/root_backup.tar.gz /* --exclude "/dev" --exclude "/proc" --exclude "/sys" --exclude "/tmp" --exclude "/lost+found" --exclude "/home/$USER/root_backup.tar.gz"

In this way we have created the archive containing all the files in the “/” excluding /dev/proc/sys/tmp/lost+found and the same store that you will create.

Specifications

Let’s see in detail the options used in this guide.

c = creates the archive

v = verbose mode, it shows the complete output

f = use file archive

x = extract the archivio

z = squeeze using gzip

j = squeeze using bzip2

t = view contents

-C directory = specifies the destination directory

–exclude = exclude files or directories

For more options and functionality see the tar manual available with the command:

$ man tar

Good Storage!

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.