The Tar command in Linux: file and compress files

Introduction

The tar command is one of the oldest and most versatile Linux tools to group multiple files into a single file, known as a tarball. Although not compressed on its own, tar is often combined with gzip, bzip2 or xz to get compressed files that occupy less space and are easier to transfer.

Basic syntax

The general form of tar is:

tar [opciones]  [archivos o directorios]

The most common options are:

  • -c: create a new file.
  • -x: extract files from a tarball.
  • -t: list the unextracted content.
  • -v: verbose mode, displays processed files.
  • -f: specifies the name of the tarball file.

Create a no-compression tarball

To group several files or a directory into a tar file without compressing:

tar -cvf respaldo.tar /home/usuario/documentos

This createsrespaldo.tarand displays each file added thanks to the option-v.

Extract a tarball

To restore the files:

tar -xvf respaldo.tar

If you want to extract in a different directory, use-Cfollowed by the route:

tar -xvf respaldo.tar -C /tmp/restaurar

Gzip compression

The most used combination is tar with gzip, identified by the extension.tar.gzor.tgz:

tar -czvf copia.tar.gz /var/www

The options-zactive gzip,-ccreate,-vverbose and-findicates the file.

Bzip2 compression

For a higher compression rate (at longer cost) bzip2 is used:

tar -cjvf copia.tar.bz2 /var/www

The option-jindicates bzip2.

Compression with xz

xz offers the best compression relationship, ideal for software distributions:

tar -cJvf copia.tar.xz /var/www

The option- Jactive xz.

List the contents of a compressed tarball

You can see what's inside without decompressing:

tar -tzf copia.tar.gz

For bzip2 use-tjfand for xz-tJf.

Add files to an existing tarball

Although tar is not designed to modify files directly, you can use the option-r(add) with uncompressed files:

tar -rvf respaldo.tar nuevo.txt

For tablets tarbals it is necessary to decompress, add and recompress.

Tips and good practices

  • Always test the extraction in a temporary directory before overwriting important files.
  • Use-Excludeto omit patterns, for example-excludes = '*.log '.
  • Combine withsshto create remote backup:tar -czf - / etc. | ssh user @ server 'cat > / backup / etc.tar.gz'.
  • Verifies integrity withtar -tzforgzip -tdepending on the case.

Conclusion

Domain tar allows you to efficiently manage clusters and data compressions in Linux, whether for backups, software distribution or simple file organization. Practice the different options and combine the compression algorithms will save you time and disk space.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish