5

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.

Stephen Kitt
  • 434,908
Arya
  • 316
  • 4
    Do you want to store the files in the archive with their leading / (unwise), or somehow strip it off so that the files have relative pathnames (safer)? – Chris Davies Oct 09 '19 at 20:08

3 Answers3

12

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.

  • @Hi Piat. Thanks a lot for the reply. Actually all the list of file names are available in Files.txt . So how can i get rid of this issue – Arya Oct 09 '19 at 19:15
  • 1
    I don't understand why tar returns this message to stderr. In my opinion it should return it to stdout. – mgutt Oct 20 '22 at 10:50
9

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 -.

ilkkachu
  • 138,973
  • @Solution worked. Is there any way without getting parent directory folder names and have only files inside the tar.gz file ? ( without using cd dir and do tar ) – Arya Oct 10 '19 at 02:48
3

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 /.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287