Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

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?

share|improve this question
1  
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. – N.N. Feb 15 '13 at 18:48
up vote 14 down vote accepted

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.

share|improve this answer
    
You could also use a relative path. – N.N. Feb 15 '13 at 18:46
    
@N.N. Absolutely, but since we have no idea what $PWD is, it seemed safest to use an absolute path in the example. – Michael Kjörling Feb 15 '13 at 18:49
    
Indeed, I just though that it made sense to include that to show that it is an available option for solving the problem. – N.N. Feb 15 '13 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. – mouviciel Dec 13 '13 at 8:50

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.

share|improve this answer

tar cfz /save/dir/file1.tar.z -C /dir/to/locate/ file1

this will put your tar ball file into the specified directory and will create tar ball with the relative path to the actual source file location.

share|improve this answer
1  
This is nearly identical to user2039152's answer. – don_crissti Feb 25 at 18:47

I turn the compressed data into a stream (-) and easily rename and locate (>) whereever I choose (Also I 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

share|improve this answer

Try changing your directory to the location you want your tar. Then create your tar file. Works for me.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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