1

I'm trying to combine two options for creating a tar archive, but they don't work well together. I don't know what's the problem.

I use:

tar -C /var/www/website html -cvzf html.tgz -T /tmp/htmllist.txt

If I use only the -C option it creates a tar archive with only a relative path "./html/", but ALL the files inside that folder. I don't want this.

Then if I use only the -T option it creates a tar archive with only the required files, but the path is absolute "/var/www/website/html/whatever.html". I don't want this either.

I want a combination of the two, but somehow it doesn't work.

The folder structure is:

/var/www/website/html/
  • Lots of .html files

The /tmp/htmllist.txt folder contains lines like:

  • /var/www/website/html/file1.html
  • /var/www/website/html/file5.html
  • /var/www/website/html/file32.html

And so on.

How do I solve this problem?

  • 1
    If you cd /var/www/website and use relative file paths in /tmp/htmllist.txt then tar will use relative paths in the archive. You can also use find and pipe the output to tar ... -T -. – AFH Jun 4 '15 at 12:36
1

Tar works the other way around when using -T. It archives the files including full path. If you don't want the path, strip it on extracting, for example,

Create tarball

tar -C /var/www/website html -cvzf html.tgz -T /tmp/htmllist.txt

Extract tarball

tar -xvf html.tgz --strip-path=3

This strips three components from the output path, i.e. /var/www/website.

  • Thanks for your answer! Well, this tarball is generated for the web users to download so I don't expect them to use strip path or any complicated thing. Plus I don't want them to read the full path. Is there anyother solution? – DMCGIVEN Jun 4 '15 at 12:14
  • Or maybe I can compress, extract, and recompress again ? This is unefficient though.... – DMCGIVEN Jun 4 '15 at 12:18
  • The non-obvious solution would be to change into the source directory and do something like tar -cvzf foo.tgz *.html – bjanssen Jun 4 '15 at 12:28
  • It's not *.html It's a specific selection of certain .html files – DMCGIVEN Jun 4 '15 at 12:55

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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