How to tar a directory to a different directory

gziprheltar

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' or tar –usage' for more information.

I'm on RHEL 6.7

Best Answer

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
Related Question