# L'archivage et la compression sur Linux

Sur Linux, on utilise la commande **tar** pour créer des archives au format **.tar**.

Par défaut, une archive n’est **pas compressée**. Il faudra utiliser un logiciel de compression (gzip, bzip2 ou xz) pour compresser l’archive, ce qui donnera une archive au format **.tar.gz**, **.tar.bz2** ou **.tar.xz**.

Heureusement, la commande **tar** – incluse nativement dans toutes les distributions Linux ([Ubuntu](https://lecrabeinfo.net/installer-ubuntu-18-04-lts-dual-boot-windows-10.html), [Debian](https://lecrabeinfo.net/installer-linux-debian-le-guide-complet.html)…) – gère la compression. Elle permet ainsi de créer une archive .tar, puis de la compresser avec un logiciel de compression comme gzip et ce, **en une seule commande**.

Pour résumer :

- **tar** est un logiciel d’archivage qui permet de combiner plusieurs fichiers en un seul.
- **gzip** est un logiciel de compression utilisé pour réduire la taille d’un fichier.
- **tar** et **gzip** sont utilisés ensemble pour créer des archives compressées.
- .tar : fichier d’archive non compressé.
- .gz : fichier (archive ou non) compressé avec gzip.
- .tar.gz : fichier d’archive compressé avec gzip.

> Quelle est la différence entre gzip, bzip2 et xz ?

- gzip utilise l’algorithme de compression **Deflate**.
- bzip2 utilise l’algorithme de compression du même nom (**bzip2**).
- xz utilise l’algorithme de compression **LZMA2** (comme [7-Zip](https://lecrabeinfo.net/compresser-fichier-ou-dossier-avec-7-zip-windows.html)).
- Au niveau des performances : 
    - Vitesse d’extraction (du + rapide au + lent) : **gzip** &gt; xz &gt; bzip2.
    - Vitesse de compression (du + rapide au + lent): **gzip** &gt; bzip2 &gt; xz.
    - Taux de compression (du meilleur au moins bon) : **xz** &gt; bzip2 &gt; gzip.
    - Compatibilité (Linux) : **gzip** &gt; bzip2 &gt; xz.
    - Compatibilité (Windows) : **xz** &gt; gzip, bzip2

#### Compresser un fichier ou un dossier

Pour compresser un **répertoire entier** ou bien un **seul fichier**, entrez la commande suivante :

```bash
tar -czvf nom-de-l-archive.tar.gz /chemin/vers/répertoire-ou-fichier
```

<p class="callout info">La compression fonctionne de manière **récursive** : elle compresse tous les fichiers et sous-dossiers du répertoire que vous avez spécifié.</p>

Voici le détail des options :

- –**c** : crée un archive.
- –**z** : compresse l’archive avec gzip.
- –**v** : mode verbeux, affiche la progression.
- –**f** : permet de spécifier le nom du fichier d’archive.

#### Compresser plusieurs fichiers à la fois

Pour compresser un **répertoire entier** et **des fichiers**, entrez la commande suivante :

```bash
tar -czvf archive.tar.gz ~/chemin/repertoire/ ~/chemin/fichier.jpg ~/chemin/fichier2.odt
```

#### Décompresser une archive

La commande tar est aussi capable d’**extraire** (décompresser) une archive.

Par exemple, pour extraire le contenu du fichier archive.tar.gz dans le **répertoire courrant**, entrez la commande suivante :

```bash
tar -xzvf archive.tar.gz
```

<p class="callout info">Il s’agit en fait de la même commande que celle pour créer une archive, sauf que l’on remplace l’option -c par **-x** qui indique à `tar` d’extraire une archive au lieu d’en créer une.</p>

Pour extraire le contenu de l’archive dans un **répertoire spécifique**, il faudra ajouter l’option **-C**. Par exemple, pour extraire le contenu du fichier archive.tar.gz dans le répertoire /tmp, entrez la commande suivante :

```bash
tar -xzvf archive.tar.gz -C /tmp
```

## Bonus: cheat sheet

##### Compression

<div class="table-responsive" id="bkmrk-commande-description"><table class="table table-bordered table-striped table-hover"><thead class="thead-light"><tr><th>Commande</th><th>Description</th></tr></thead><tbody><tr><td>`tar -<strong>cz</strong>vf archive.tar.gz [fichier(s)]`</td><td>Compresser avec tar + **gzip**</td></tr><tr><td>`tar -<strong>cj</strong>vf archive.tar.bz2 [fichier(s)]`</td><td>Compresser avec tar + **bz2**</td></tr><tr><td>`tar -<strong>cJ</strong>vf archive.tar.xz [fichier(s)]`</td><td>Compresser avec tar + **xz**</td></tr></tbody></table>

</div>##### <span class="h3">Extraction</span>

<div class="table-responsive" id="bkmrk-commande-description-0"><table class="table table-bordered table-striped table-hover" style="width: 100%; height: 167.188px;"><thead class="thead-light"><tr style="height: 41.7969px;"><th style="width: 49.9383%; height: 41.7969px;">Commande</th><th style="width: 49.9383%; height: 41.7969px;">Description</th></tr></thead><tbody><tr style="height: 41.7969px;"><td style="width: 49.9383%; height: 41.7969px;">`tar -<strong>xz</strong>vf archive.tar.gz`</td><td style="width: 49.9383%; height: 41.7969px;">Extraire une archive **gzip**</td></tr><tr style="height: 41.7969px;"><td style="width: 49.9383%; height: 41.7969px;">`tar -<strong>xj</strong>vf archive.tar.bz2`</td><td style="width: 49.9383%; height: 41.7969px;">Extraire une archive **bz2**</td></tr><tr style="height: 41.7969px;"><td style="width: 49.9383%; height: 41.7969px;">`tar -<strong>xJ</strong>vf archive.tar.xz`</td><td style="width: 49.9383%; height: 41.7969px;">Extraire une archive **xz**</td></tr></tbody></table>

</div>##### <span class="h3">Lister les fichiers d’une archive  
</span>

<div class="table-responsive" id="bkmrk-commande-description-1"><table class="table table-bordered table-striped table-hover"><thead class="thead-light"><tr><th>Commande</th><th>Description</th></tr></thead><tbody><tr><td>`tar -<strong>tz</strong>vf archive.tar.gz`</td><td>Lister les fichiers d’une archive **gzip**</td></tr><tr><td>`tar -<strong>tj</strong>vf archive.tar.bz2`</td><td>Lister les fichiers d’une archive **bz2**</td></tr><tr><td>`tar -<strong>tJ</strong>vf archive.tar.xz`</td><td>Lister les fichiers d’une archive **xz**</td></tr></tbody></table>

</div>