How to overcome tar: Removing leading `/' from member names

I am trying to run this command in Linux:

tar zcvf ABCD.tar.gz -T Files.txt

and I am getting the following error:

Error: tar: Removing leading `/' from member names

Based on Find /SED to convert absolute path to relative path within a single line tar statement for crontab, I tried this command:

tar -C / -zcvf ABCD.tar.gz -T Files.txt

but I am still getting the same error message.

Asked By: Arya

||

This is a feature!

If the slash / prefix was included in the archive during compression and extraction, it means that an attacker would just have to convince you to extract the file to overwite a sensitive file (like /etc/shadow).

It isn’t actually an error message, more an information.

If you really want to suppress it, change directory to the parent directory and use relative names:

cd /
tar -zcvf path/to/files

Rather than

tar -zcvf /path/to/files

In your case, I suppose the files names in the -T Files.txt are absolute location.

Answered By: Franklin Piat

In GNU tar, if you want to keep the slashes in front of the file names, the option you need is:

   -P, --absolute-names
          Don't strip leading slashes from file names when creating archives.

So, tar zcvf ABCD.tar.gz -P -T Files.txt.

The slashes would probably be removed when the archive is extracted, unless of course you use -P there, too.

If, on the other hand, you want to remove the slashes, but without tar complaining, you’d need to do something like sed s,^/,, files.txt | tar czf foo.tar.gz -C / -T -.

Answered By: ilkkachu

Another possible answer is that you really do want your archive to have pathnames that are safe to extract, and you want to strip the leading slash from the list of files you have.

In this case there is no option in tar, but you can generate tar-compatible files with pax.

pax -s '#^/##' -wz < Files.txt > ABCD.tar.gz

The -s '#^/##' applies a substitution pattern to the filenames as they are written to the archive. This one strips a leading /.

Answered By: roaima
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.