La commande tar

Sommaire

Index

Utilisation basique de la commande tar

Création

tar cf archive.tar dossier

Affichage du contenu

tar tf archive.tar

Compression

gzip archive.tar

Compression à la création

tar czf archive.tar.gz dossier

Changement de dossier cible

Il est possible d’indiquer à la commande tar un dossier différent du répertoire courant pour réaliser ses opérations.

Pour les exemples suivants, nous allons créer une arborescence de test:

mkdir -p data/info
echo "Informations" > data/info/fichier1.txt
echo "# Présentation" > data/README.md

Ce qui donne l’arborescence suivante:

$ tree
.
└── data
    ├── info
    │   └── fichier1.txt
    └── README.md

Compression du dossier data et de son contenu

tar czf data.tar.gz data

Contenu du fichier data.tar.gz

La commande tar tf data.tar.gz donne la sortie suivante:

data/
data/info/
data/info/fichier1.txt
data/README.md

Ceci montre que le dossier data a été inclus au moment de la création de l’archive.

Compression du contenu seulement

Bien noter le . à la fin de la commande:

tar czf donnees.tar.gz -C data .

La commande tar tf donnees.tar.gz donne la sortie suivante:

./
./info/
./info/fichier1.txt
./README.md

Décompression dans un dossier cible

L’option -C suivie du chemin cible permet de décompresser dans un dossier différent du dossier courant:

mkdir chemin/vers/dossier_cible
tar xf donneez.tar.gz -C chemin/vers/dossier_cible

Exemples

Archivage d’une arborescence

Archivage du dossier /usr/bin

tar czf usr-bin.tar.gz /usr/bin

L’archive contient dans ce cas l’arborescence complète usr/bin:

$ tar tf usr-bin.tar.gz | head -3
usr/bin/
usr/bin/rev
usr/bin/diff3

Archivage d’un dossier (et de ses sous-dossiers)

Archivage du dossier bin situé dans /usr

tar czf bin.tar.gz -C /usr bin

Dans ce cas, seul la partie bin de l’arborescence est stockée:

$ tar tf usr-bin.tar.gz | head -3
bin/
bin/rev
bin/diff3

Exercices

  1. Reprendre l’arborescence créée dans le chapitre droits et en créer une archive
cd informatique
tar czf informatique.tar.gz mac/ linux/ windows/

autre solution:

tar czf informatique.tar.gz -C informatique .
  1. Extraire cette archive dans le dossier /tmp

Méthodes de compression

BZip2

tar cjf archive.tar.bz2 dossier

xz

Le J est majuscule !

tar cJf archive.tar.xz dossier

Visualiser des fichiers compressés

cat > fichier <<EOF
ENDNOTES:
(1)  "Waskstone", see Adventure XXXV, note 2.
(2)  "Fall".  The word "not", translated here "fall", means
     really 'disaster', but as this word is not in keeping with
     the style, "fall" has been chosen as preferable to 'need',
     used by some translators.  The MS. C has here "liet" instead
     of "not" of A and B.
(3)  The "Nibelungenlied" is continued by the so-called "Klage",
     a poem written in short rhyming couplets.  As the name
     indicates, it describes the lamentations of the survivors
     over the dead.  The praises of each warrior are sung and a
     messenger dispatched to acquaint Gorelind, Uta, and Brunhild
     with the sad end of their kinsmen.  It closes with
     Dietrich's departure from Etzel's court and his return home.
     Although in one sense a continuation of our poem, the
     "Klage" is an independent work of no great merit, being
     excessively tedious with its constant repetitions.  A
     reprint and a full account of it will be found in Piper's
     edition of our poem, vol. I.
EOF
gzip fichier
zcat fichier.gz | less