Thursday, June 2, 2022

[SOLVED] Unix tar: do not preserve full pathnames

Issue

When I try to compress files and directories with tar using absolute paths, the absolute path is preserved in the resulting compressed file. I need to use absolute paths to tell tar where the folder I wish to compress is located, but I only want it to compress that folder – not the whole path.

For example, tar -cvzf test.tar.gz /home/path/test – where I want to compress the folder test. However, what I actually end up compressing is /home/path/test. Is there anything that can be done to avoid this? I have tried playing with the -C operand to no avail.


Solution

Use -C to specify the directory from which the files look like you want, and then specify the files as seen from that directory:

tar -cvzf test.tar.gz -C /home/path test


Answered By - Mark Reed
Answer Checked By - Marie Seifert (WPSolving Admin)