I am taring a folder and for some reason the entire directory structure that preceds the folder I am tarring is included.

I am doing this in a script like:

'tar czf ' + dir + '/asdf.tgz ' + dir + 'asdf/'

Where dir is like:


In the man pages, I see I can fix this but I can't get it to work.

I tried:

tar czf -C dir ...

But now I have some kind of a file -C in my folder (which I can't seem to delete btw!).

Please help!

  1. The single-character options must be followed immediately by their parameters, then any other parameters. Otherwise, use normal style options or combine the -C the same way:

    tar cvCf dir asdf.tgz asdf
    tar -c -v -f dir/asdf.tgz -C dir asdf
  2. Since programs read - as meaning an option character, you need to defeat this to access a file starting with -. Two ways to do this:

    rm -- -C
    rm ./-C
| improve this answer | |
  • I'm getting a failed to open '~/path/to', here is my command: tar czfC ~/path/to/asdf.tgz asdf/ – Blankman Apr 12 '12 at 22:20
  • That command doesn't look right; as written you have told it to use archive ~/path/to/asdf.tgz, then change directory to asdf/, but no files to add to the archive (which should produce an error message). – geekosaur Apr 12 '12 at 22:29
  • ok I don't understand why the order matters, I thought cvCf are just options that you are passing, compress, verbose, f etc... – Blankman Apr 13 '12 at 20:44
  • C and F both want pathname arguments. They don't mean the same thing, so you'd kinda like to know which name goes with which option, yes? – geekosaur Apr 13 '12 at 20:56
  • @Blankman or just change to the dir first. cd ~/Downloads/archive/ && tar -czf asdf.tar.gz asdf/ – geirha Apr 15 '12 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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