1. Introduction
The ubiquitous Linux tar command has many features. Part of them are explicit and may add complex functions, while others are implicit and are there to protect the user.
In this tutorial, we discuss a tar warning message about leading slashes and absolute paths. First, we check the default extraction mode. After that, we turn to leading slashes and how tar handles them. Finally, we check ways to force absolute paths when using tar.
For brevity, we only discuss the latest GNU tar implementation available as a package in Debian.
We tested the code in this tutorial on Debian 11 (Bullseye) with GNU Bash 5.1.4 and GNU tar 1.34. It should work in most POSIX-compliant environments.
2. Archive Extraction and Paths
As an archiving solution, tar enables us to extract a given archive to the current path with its –extract or -x option:
$ tar -xvf archive.tar
data/
data/subdir/
data/subdir/file
In addition, we use –file or -f to supply the archive path as archive.tar and –verbose or -v to show the extracted files.
Notably, all extracted paths above begin with a directory name and not a leading / slash, i.e., they are relative, not absolute. This means the resulting data ends up below the current directory.
Now, we can check what the archive contains:
$ tar -tf archive.tar
data/
data/subdir/
data/subdir/file
Again, the –list or -t option shows only relative paths.
3. Leading Slashes
Let’s see how we created archive.tar in the first place:
$ tar -cvf archive.tar /data/
tar: Removing leading `/' from member names
/data/
/data/subdir/
/data/subdir/file
Here, we see the leading slashes in all archived paths. However, we can also notice a stderr message about the removal of these leading slashes.
Actually, this warning is critical, as it shows what a hardcoded feature of tar produces: by default, absolute paths are converted to relative paths when archiving.
So, when we extract, we get the files under our current path instead of at their original locations.
4. Using Absolute Paths
To preserve the absolute paths of archive members, we can use the –absolute-names or -P option when creating archive-abs.tar:
$ tar -cPvf archive-abs.tar /data/
/data/
/data/subdir/
/data/subdir/file
Notably, with -P we don’t get the tar: Removing leading `/’ from member names warning.
Let’s verify the contents of archive-abs.tar:
$ tar -tf archive-abs.tar
tar: Removing leading `/' from member names
/data/
/data/subdir/
/data/subdir/file
Unlike before, we see the leading slashes. However, the message from earlier has reappeared. In fact, this is another safety precaution, preventing us from operating with archive members as absolute paths. Even a simple listing triggers the warning.
To force tar to work with the absolute paths after creating an archive with such, we again use the –absolute-paths or -P flag. Let’s see an example with extraction:
$ tar -xPvf archive-abs.tar
/data/
/data/subdir/
/data/subdir/file
Thus, the archive has been extracted into the data directory at the / root of the filesystem without warnings. Critically, the behavior can cause unexpected data loss when involving system paths like /home, /dev, /opt, and others.
5. Summary
In this article, we explored leading slashes and absolute paths, as well as their consequences when using tar.
In conclusion, due to the potentially serious consequences of using absolute paths, tar automatically prevents this behavior unless explicitly requested.