# How to tar a directory to a different directory?

How do I go about compressing a folder (tar1) and sending that compressed folder to a different directory (lets say called tar2).

I checked the questions on here, and most of them are using a file, not a directory and I've been trying all combinations, but I can't get it to work right.

In my ~ I have:

tar1/
a.txt
b.txt
tar2/


Tar1/ has a test file called a.txt and b.txt in it. I want to compress that folder and place it in tar2/

So afterwards it would look like:

tar1/
a.txt
b.txt
tar2/
tar1.gz
a.txt
b.txt


I was trying

tar czvf tar2/ tar1/


(A reference book I had gave an example in that syntax, where the first path is the place you want to store it, and the 2nd path is what you want to create the archive of.

I've also tried somethings with -C as my destination:

tar cvzf tar1/ -C tar2/


Hoping that that would take tar1/ compress the directory and place that in tar2

I got this error

tar: Cowardly refusing to create an empty archive

Try tar --help' ortar --usage' for more information.

I'm on RHEL 6.7

• You can't tar to a directory - you have to tar to a file. You can tell tar to put that file in any directory you want, but it has to be a file. – John Oct 14 '16 at 15:10

You are not specifying an archive in your statements. It should look something like:

tar -cvf tar2/tar1.tar tar1/


This places the tarball tar1.tar inside the directory tar2/.

Before:

tree tar*
tar1
├── a.txt
└── b.txt
tar2
├── a.txt
└── b.txt

0 directories, 4 files


After:

tar -cvf tar2/tar1.tar tar1/
tar1/
tar1/a.txt
tar1/b.txt

tree tar*
tar1
├── a.txt
└── b.txt
tar2
├── a.txt
├── b.txt
└── tar1.tar

0 directories, 5 files


Environment:

Distributor ID: Debian
Description:    Debian GNU/Linux 8.6 (jessie)
Release:    8.6
Codename:   jessie

• Thanks for the help! To add one more piece to the puzzle (I didn't specify this since I thought the command would be simpler).. I won't able to use the mv command due to space constraints. – robromo Oct 14 '16 at 15:14
• (hit enter too fast) The overall goal is I am trying to help my team migrate files from our old server to our new server that is replacing it. We are trying to preserve the owner of the files intact (wary about if we dont UID's will cause errors....) We tried RYSNC and couldnt preserve owners.... We thought mounting and tar would. So I am trying to create a mount while on the old machine to the new one, and then be able to tar up a folder and place it into a folder on the new machine (which will be visible because mount). It needs to go directly to the new machine due to space. – robromo Oct 14 '16 at 15:17
• I.e. The old machine has lets is 100gb, and we have 60gb of data i want to tar up, assuming the tar is 50gb it wont be able to fit, hence why I have to have that tar get saved in the folder that I mounted too on the new machine. – robromo Oct 14 '16 at 15:18
• See my edit. I guess this is what you want? – maulinglawns Oct 14 '16 at 15:20
• I tried that (on a test folder to see how it behaved) not on the real one, but I think it worked! – robromo Oct 14 '16 at 15:28

What you are actually trying to do is buried in a comment to another answer. tar is obviously not a pre-requisite, nor is compression really.

So here's how to do that with pax.

To create a compressed archive of tar1/* (but without the tar1/ prefix) in tar2/tar1.tar.gz:

( cd tar1/ && pax -z -w * > ../tar2/tar1.tar.gz )

To copy the entire hierarchy directly from tar1/ to tar2/ without an intermediate archive and preserving everything:

( cd tar1/ && pax -p e -r -w * ../tar2/ )

Note that the shell will not include dot files in its expansion of *, of course. Handle that in the usual ways if it happens to be a problem.