Tutoriel Linux

Tar under Linux: creating and extracting an archive without losing paths

Débutant5 min de lecture
À retenirLinux n'est pas réservé aux experts. Le bon point de départ : une distribution accessible, une sauvegarde propre et quelques commandes comprises.

You have retrieved an archive tar Or tar.gzYou want to open it properly, or you need to package a folder before moving it. The command seems simple, until the moment the archive recreates an entire directory tree in the wrong place or overwrites a file you wanted to keep.

I advise you to treat tar This guide will show you how to create an archive, list its contents, extract it to a chosen folder, and verify the result before touching the production files.

Tux organizes a Linux tar archive with folders and abstract paths.
With tar, the destination folder and saved paths are just as important as the command itself.

Tar is used for archiving, not necessarily for compression.

The key point to keep in mind: tar groups files and preserves their pathsCompression is often added on top, using gzip, xz, or bzip2. That’s why you come across files in .tar, .tar.gz, .tgz, .tar.xz Or .tar.bz2.

Before extracting an unknown archive, do not directly run a random command in your personal folder or in /var/wwwList its contents. You will see if it contains a clean root folder, several flat files, or longer than expected paths.

tar -tf backup.tar

tar -tzf backup.tar.gz

tar -tJf backup.tar.xz

THE t The list contains the contents. f indicates the archive file. The options z And J they respectively indicate gzip and xz.

Create a clean tar archive

To create an uncompressed archive of a folder, first navigate to the correct directory level. This is important because tar records the paths you give it.

cd /home/nathan/projets

tar -cf site.tar site

This command creates site.tar with the file site inside. If you extract it later, you will retrieve a folder site/, not a scattering of files in the current directory.

To create a gzip compressed archive, add z :

tar -czf site.tar.gz site

For xz, which is often more compact but slower, use J :

tar -cJf site.tar.xz site

On a server, I sometimes prefer a simple gzip archive. It’s quick to create, readable everywhere, and more than sufficient for moving a configuration folder or preparing a one-off backup.

Avoid absolute paths in an archive

A bad habit is to record an absolute path without thinking:

tar -czf site.tar.gz /var/www/site

Tar usually knows how to remove the / Initially, this will warn you, but it’s not the best basis for a clean transfer. You also risk not knowing exactly where the files will end up after extraction.

I prefer to use -C to clearly tell tar where to position itself before reading the files:

tar -czf /tmp/site.tar.gz -C /var/www site

Here, the archive contains site/, not var/www/site/It’s more readable, more portable, and much less of a hassle to extract onto another machine.

Extract an archive to the correct folder

To extract an archive .tar in the current folder:

tar -xf site.tar

For a gzip archive:

tar -xzf site.tar.gz

But in practice, I often recommend preparing an empty folder and extracting the files into it. You can immediately see what the archive contains, without cluttering the current directory.

mkdir -p /tmp/test-extraction

tar -xzf site.tar.gz -C /tmp/test-extraction

find /tmp/test-extraction -maxdepth 2 -type f | head

This method avoids many unpleasant surprises, especially with an archive downloaded or sent by someone else.

See what will be crushed before extracting

Tar can replace existing files if the paths match. On a test machine, this isn’t a problem. On an application folder, it can quickly become a real headache.

Before extracting to a folder that already contains files, list the archive and then compare it with the destination:

tar -tzf site.tar.gz | head

ls -la /var/www/site

If you want to test without mixing files, keep using a temporary folder:

rm -rf /tmp/test-extraction
mkdir -p /tmp/test-extraction

tar -xzf site.tar.gz -C /tmp/test-extraction

diff -qr /tmp/test-extraction/site /var/www/site

THE diff -qr It does not replace a real backup strategy, but it already gives a clear idea of ​​the different files.

Extract without recreating the entire first folder

Some projects come with a root folder of the type app-1.4.2/If you only want to retrieve its content to a specific destination, --strip-components=1 may be useful.

mkdir -p /tmp/app

tar -xzf app-1.4.2.tar.gz -C /tmp/app --strip-components=1

Warning: Do not use this option without first listing the archive. If the archive does not contain the expected folder level, you may obtain a different result than you intended.

Exclude a file from an archive

When backing up an application folder, you don’t always want to include caches, large logs, or regenerable dependencies. The option --exclude allows the archive to be kept cleaner.

tar -czf site.tar.gz --exclude='site/cache' --exclude='site/logs' site

If you are preparing a proper backup, do not replace a controlled rsync synchronization by creating a manual archive. `tar` is convenient for packaging, transporting, or freezing a folder. For regular backups, you should also consider rotation, restore testing, and separate storage.

Tar.gz file to install: do not confuse archive and program

A file .tar.gz It may contain software, but that doesn’t mean it’s always installed with the same command. Sometimes it contains a ready-to-run binary, sometimes a script, sometimes source code to compile.

If your goal is truly to install software downloaded as tar.gzStart by reading the content, the file README or the publisher’s documentation. I detailed this case separately in the guide. Installing a tar.gz file under Linux without running random commands.

Before restoring the archive

Before moving or restoring an archive, check at least these points:

  • the exact content with tar -tf, tar -tzf Or tar -tJf ;
  • the destination folder with pwd And ls -la ;
  • a test extraction in /tmp when the archive comes from an external source;
  • the rights and owners after extraction with ls -l ;
  • the files modified with diff -qr if you restore over an existing folder.

If you then need to rename or move extracted files, do it properly with the mv commandNot by fiddling with an extraction into the wrong folder. A well-prepared tar archive should save you time, not leave you with a directory structure to fix manually.

To learn more, you can also consult the GNU tar manual and the page tar(1) from man7.orgThey are less digestible than a practical guide, but very useful as soon as you start using advanced options.

sudo apt update && sudo apt upgrade