brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: tar strips the leading slash from paths, and -P turns that off

Tar
~/tips/tip-tar-absolute-names-leading-slash

tar -cf etc.tar /etc/example

Stops an archive from writing to absolute paths unless you ask for it with -P.

Unpacking a tarball you did not make is a small leap of faith. Part of the safety net is a habit GNU tar has by default: it takes the leading slash off every path.

tar -cf etc.tar /etc/example

tar prints tar: Removing leading `/' from member names and stores etc/example/... as a relative path. The same happens when you extract an archive that holds absolute names. In a throwaway folder, an archive made with -P that contained a path under /tmp printed the same warning on extraction and the file landed inside the current folder, in a copy of that path.

The switch to keep the slash is -P, or --absolute-names:

tar -cPf etc.tar /etc/example
tar -tPf etc.tar

The first command stores the paths as they are, and the list starts with /.

When would I use -P on purpose? For a backup that must go back to exactly the same place, on a machine you control. For everything else, relative names are what you want, because the archive can be unpacked under any folder.

The caveat is the other direction. With -P on extraction, tar writes to the absolute location the archive names. Unpacking such an archive from a subfolder put the file in its original place, outside the current folder, and that place can be a system file. So never add -P to an extract command for an archive from someone else. List it first with tar -tPf and read the names. If they start with a slash and you did not expect that, leave it packed.

Further reading: GNU tar manual

More Tar tips printf 'Signature: 8a477f597d28d172789f06886806bc55' > /mnt/user/appdata/app/cache/CACHEDIR.TAG tar -xzf app-2.4.1.tar.gz -C /mnt/user/appdata/app --strip-components=1 All Tar tips (3) All topics

Link