How do I exclude absolute paths for tar?

Angel S. Moreno picture Angel S. Moreno · Jun 30, 2010 · Viewed 57.9k times · Source

I am running a PHP script that gets me the absolute paths of files I want to tar up. This is the syntax I have:

tar -cf tarname.tar -C /www/path/path/file1.txt /www/path/path2/path3/file2.xls

When I untar it, it creates the absolute path to the files. How do I get just /path with everything under it to show?

Answer

Johnny Baloney picture Johnny Baloney · Dec 19, 2012

You are incorrectly using the -C switch, which is used for changing directories. So what you need to do is:

tar -cf tarname.tar -C /www/path path/file1.txt path2/path3/file2.xls

or if you want to package everything under /www/path do:

tar -cf tarname.tar -C /www/path .

You can use -C switch multiple times.