61

I want to create a tar archive in a different directory rather than the current directory.

I tried this command:

tar czf file.tar.gz file1 -C /var/www/

but it creates the archive in the current directory. Why?

2
  • 4
    You seem to have misunderstood the meaning of -C (which is not strange as its documented vaguely). For details see serverfault.com/q/416002/86283. Commented Feb 15, 2013 at 18:48
  • It's worth mentioning that globing (eg. *) does not work as indented when one uses the -C flag. Because the globing will take place before tar change the directory. But for your purpose you don't need this flag as shown by the accepted answer. Commented Aug 9, 2022 at 20:35

4 Answers 4

70

The easy way, if you don't particularly need to use -C to tell tar to change to some other directory, is to simply specify the full path to the archive on the command line. Then you can be in whatever directory you prefer to create the directory structure that you want inside the archive.

The following will create the archive /var/www/file.tar.gz and put file1 from the current directory (whatever that happens to be) in it, with no in-archive path information.

tar czf /var/www/file.tar.gz file1

The path (to either the archive, the constituent files, or both) can of course also be relative. If file1 is in /tmp, you are in /var/spool and want to create the archive in /var/www, you could use something like:

tar czf ../www/file1.tar.gz /tmp/file1

There's a million variations on the theme, but this should get you started. Add the v flag if you want to see what tar actually does.

5
  • 1
    You could also use a relative path. Commented Feb 15, 2013 at 18:46
  • 2
    @N.N. Absolutely, but since we have no idea what $PWD is, it seemed safest to use an absolute path in the example. Commented Feb 15, 2013 at 18:49
  • 1
    Indeed, I just though that it made sense to include that to show that it is an available option for solving the problem. Commented Feb 15, 2013 at 18:51
  • That way, the path of file1 in the tar archive will be /tmp. This may or may not be what you expect. Commented Dec 13, 2013 at 8:50
  • What about the hard way? :) Commented Jun 29, 2021 at 18:58
13

I think, it should be:
tar czf file.tar.gz -C /var/www/ file1

Which works for me. It tells to change directory and then choose file.

2
  • 2
    Creates file.tar.gz in the current directory. -C affects only what follows it. And supposedly doesn't affect -f. tar (GNU tar) 1.34 Commented Jun 29, 2021 at 18:58
  • 1
    This creates the tar.gz in the current directory and adding file1 from /var/www without /var/www as prefix. It's worth mentioning that this is the opposite the OP wants, but is also quite useful. Also mind that glob * does not work here!! Commented Aug 9, 2022 at 20:30
10

I turn the compressed data into a stream (-) and easily rename and locate (>) wherever I choose (I also always tar the relative path (./) so it's easier to deal with when uncompressing)

tar -cvf - ./dir-to-compress/* > /location-of-new-file/filename.tar
1
  • 1
    That doesn't help with individual files, and -C is much more idiomatic (and shorter). Commented Aug 22, 2017 at 19:46
-3

Worked perfectly

cd /home; tar -czvf - /var/log/* > varlog.tar.gz

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.