This question came from our site for professional and enthusiast programmers.

What exactly is not working as expected?
–
Joachim IsakssonDec 23 '12 at 12:48

9

It is not a problem. You do not want leading slashes in a tar archive. Seriously. If you want to extract an archive to your system root, specify -C / when extracting it.
–
ThiefMasterDec 23 '12 at 15:31

That's actually a feature, not a problem. Archives with absolute locations are a security risk. Attackers could use such archives to trick users into installing files in critical system locations.

Yes, you could use -P. But what's wrong with allowing tar to remove the forward slash, and simply requiring the user of the archive to explicitly do the extraction in the root directory? Then they're consciously impacting critical system locations, and can't do it by accident.

If you want to get rid of "Removing leading `/' from member names" being printed to STDERR, but still want to leave off those leading slashes as tar wisely does by default, I saw an excellent solution here by commenter timsoft.

The solution involves using -C option to change directory to the root (/), then specifying the file tree to archive without a leading slash, because now you only need a relative path. This does the same thing as a normal tar create command, but no stripping is needed:

It won't work with incremental backups. But a good answer.
–
gajdipajtiAug 5 '14 at 7:12

It also doesn't work if you want to tar files denoted by shell-expansion (e.g. tar c -C / home/foo/*), because the shell doesn't know about the changed root. But still a good answer for other cases.
–
BorisApr 15 at 6:27