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
Extract a tarball
To restore the files:
tar -xvf respaldo.tar
If you want to extract in a different directory, use
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
Bzip2 compression
For a higher compression rate (at longer cost) bzip2 is used:
tar -cjvf copia.tar.bz2 /var/www
The option
Compression with xz
xz offers the best compression relationship, ideal for software distributions:
tar -cJvf copia.tar.xz /var/www
The option
List the contents of a compressed tarball
You can see what's inside without decompressing:
tar -tzf copia.tar.gz
For bzip2 use
Add files to an existing tarball
Although tar is not designed to modify files directly, you can use the option
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
-Exclude to omit patterns, for example-excludes = '*.log ' . - Combine with
ssh to create remote backup:tar -czf - / etc. | ssh user @ server 'cat > / backup / etc.tar.gz' . - Verifies integrity with
tar -tzf orgzip -t depending 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.


