Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The man page states

-C directory
      In c and r mode, this changes the directory before adding the following files.

However, tar does not change to the directory I specify, but instead reports

tar: <folder name>: Cannot stat: No such file or directory

for every folder in the directory I run the tar command in.

Do I really have to do something like

cd <folder> && tar ... && cd -

or is there a way to get this work ?

share|improve this question
The command line you're running is...? – Ignacio Vazquez-Abrams Jun 6 '10 at 7:03
yes, provide the commandline you are trying to get to work, not only the error messages. – akira Jun 6 '10 at 11:58
Sorry it took me so long to get back to this. The command line is not very spectacular: – ssc Sep 25 '10 at 10:28

I think you cannot use the -C option like you presume in this question.

Section 6.10.1 Changing the Working Directory in the GNU manual for tar describes the -C option like this,

To change the working directory in the middle of a list of file names, either on the command line or in a file specified using ‘--files-from’ (‘-T’), use ‘--directory’ (‘-C’). This will change the working directory to the specified directory after that point in the list.

‘-C directory’

Changes the working directory in the middle of a command line.

For example,

$ tar -c -f jams.tar grape prune -C food cherry

will place the files ‘grape’ and ‘prune’ from the current directory into the archive ‘jams.tar’, followed by the file ‘cherry’ from the directory ‘food’. This option is especially useful when you have several widely separated files that you want to store in the same archive.

Note that the file ‘cherry’ is recorded in the archive under the precise name ‘cherry’, not ‘food/cherry’. Thus, the archive will contain three files that all appear to have come from the same directory; if the archive is extracted with plain ‘tar --extract’, all three files will be written in the current directory.

There are a few more examples at the manual page...

I usually do not mix up files from different paths into a single tar-ball.
However, if that is what you desire, there are two things that can be done,

  1. Use the -T option with a list of directories/files you want to pack together.
    That will collect all things together with extra path-prefixes.
  2. Use the -h option on a 'link image' directory
    (you make one of those with a script to create links to files and directories of interest under a new and empty directory)
    The -h option used on such a directory will create a single tar-ball that represents the file layout under that directory.
share|improve this answer
i'm not sure OS X uses GNU tar (unless the OP has installed it specifically). i'd expect it provides a BSD tar by default. (tho the example syntax given is a good one to test with.) – quack quixote Jun 6 '10 at 7:18
I did read that. Tried out the example and it works. Seems like I can not use wildcards with -C to add all the files in the directory I jumped to, i.e. -C food * – ssc Jun 6 '10 at 7:24
No, indeed, the * will be expanded by the shell based on your current directory, before it's handed to tar – Henno Jun 6 '10 at 9:18

Personally, I prefer using subshells to get tar into "position". I've never learned the -C option, so I'm not sure how it works.

Assume my CWD is /path/a, and I want to create a tarball of /path/b/foo without the leading /path/b:

user@host:/path/a$ ( cd /path/b && tar czf ~/tarball.tgz foo )

The resulting tarball contains the directory foo/ and all its contents, but not the leading /path/b. The subshell executes the cd, then the tar if the cd succeeds, then exits back to my original shell in the same CWD.

share|improve this answer
I might have to resort to that solution, but there's reasons why I would love to get the -C option going. – ssc Jun 6 '10 at 7:25

tar -C doesn't work with wildcards because as Henno stated in one of the comments:

* will be expanded by the shell based on your current directory, before it's handed to tar

So if you are trying to do something like:

tar -cvzf myfile.tgz -C somepath *

It won't work. As far as I can tell you will have to change directory first like quack quixote suggests.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .