How to stop tar from archiving the root path to files?

7

1

When I run the following command:

tar cfvz backup.tar.gz /path/to/my/files/*

It gzips the entire path. So when I unzip the file, I have to go open 4 folders (path -> to -> my -> files) to get to the files I wanted to backup.

How do I make the files show in root of the gzipped file rather then the entire path?

Thanks in advance!

Ben Sinclair

Posted 2013-05-27T12:13:28.620

Reputation: 173

3Have you tried: cd /path/to/my/files and then tar czvf backup.tgz * – cfreire – 2013-05-27T12:25:31.623

3The --strip-components option while un-taring would also help. Other than that, tar doesn't offer you much… cd ing to the path you want to backup would be the only quick solution that comes to mind. – slhck – 2013-05-27T12:26:41.977

@cfreire @slhck Can't believe I didn't think of cd....Thanks so much! – Ben Sinclair – 2013-05-27T13:13:14.130

@cfreire, slhck would one of you post that as an answer? – terdon – 2013-05-27T13:16:13.907

Answers

10

Try option -C:

tar cvzf backup.tar.gz -C /path/to/my/files/ .

scai

Posted 2013-05-27T12:13:28.620

Reputation: 882

1

I'm assuming this is in a script so try something like...

pushd /path/to/my/files
tar cfvz backup.tar.gz *
popd

Tar will only include the path specified on the command-line.

Dave Noonan

Posted 2013-05-27T12:13:28.620

Reputation: 359