How do I exclude absolute paths for tar? How do I exclude absolute paths for tar? bash bash

How do I exclude absolute paths for tar?


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.


If you want to remove the first n leading components of the file name, you need strip-components. So in your case, on extraction, do

tar xvf tarname.tar --strip-components=2

The man page has a list of tar's many options, including this one. Some earlier versions of tar use --strip-path for this operation instead.


For me the following works the best:

tar xvf some.tar --transform 's?.*/??g'

--transform argument is a replacement regex for sed, to which every extracted filepath is fed. Unlike --strip-components, it will remove all path information, not just fixed number of components.