Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
root@server # tar fcz bkup.tar.gz /home/foo/
tar: Removing leading `/' from member names

How can I solve this problem and keep the / on file names ?

share|improve this question

migrated from stackoverflow.com Dec 23 '12 at 15:32

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

What exactly is not working as expected? – Joachim Isaksson Dec 23 '12 at 12:48
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. – ThiefMaster Dec 23 '12 at 15:31
up vote 22 down vote accepted

Use the --absolute-names or -P option to disable this feature.

share|improve this answer
This is the correct answer, but be aware, that in most cases, this is not what you want, cause it results in an archive that extracts in complete paths! – rubo77 Nov 21 '13 at 10:04
Using the -C / option as described in @Marcus' answer will git rid of the STDERR message if that is your primary goal. – Matt Sanders Dec 28 '13 at 22:29

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.

share|improve this answer
Sometimes features are problems. I found this thread after setting up a backup script that would email on a non-zero exit status from tar. This particular message was causing tar to exit with a status of 1, which was causing me to receive false email alerts on successful backups, simply because tar was writing this message to STDERR. I fixed this issue by using my own tweaked version of @Marcus's solution: cd /path/to/network/share && tar -cJf scripts.backup.tar.xz -C / home/user/scripts 2>/dev/null || [send an email alert] – rubynorails Dec 14 '15 at 19:14

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:

tar fcz bkup.tar.gz -C / home/foo/
share|improve this answer
It won't work with incremental backups. But a good answer. – gajdipajti Aug 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. – Boris Apr 15 '15 at 6:27
-C / doesn't work for me, period. It doesn't prevent the stderr for me. – A-B-B Aug 10 '15 at 7:45

One month late, but I found the most appropriate solution for my case (in a shell script) is to go to the parent directory and execute the command there.

cd /var/www/
tar -czf mysite.gz mysite

Instead of:

tar -czf /var/www/mysite.gz /var/www/mysite
share|improve this answer

This is how I did it by using brute force method: 2>&1 | grep -v "Removing leading".

For example:

tar -cf "$BKUPDIR/${BKUPFILE}.tar" --overwrite --exclude '.*' --one-file-system "$SRCDIR" 2>&1 | grep -v  "Removing leading"
share|improve this answer
The problem with this is it hides the error code. So if you want to check the error code from tar in a bash script, then it won't return 0 on success. – Brian Apr 7 '15 at 15:19
@staticx, the error code is 0 regardless. – A-B-B Aug 10 '15 at 7:58
@A-B-B My tar command may return 2 on fatal error. – Jite Nov 2 '15 at 15:46

This isn't a security risk. It's a safety feature at best. If you extract tarballs without prior checking the contents, you don't belong on a server anyway. Because then you are the security risk.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.