Copying directory structures using the UNIX tar command

I'm trying to create an archive of a directory structure using the tar command. I thought I'd found a construct that worked, but I'm having trouble restoring the structure from the archive. I've included an example. Notice that when I list the content of the archive just created, only the last directory is listed. Is my command missing an option?
nodename:/home/155477> find . -type d | xargs -e -i tar cvDf /tmp/my-dir.tar {}
a .
a ./.ssh
a ./my-dir
a ./Directory
a ./man1
a ./Test-Data
a ./Test-Data/archive
a ./Test-Data/Log
a ./testdir
a ./PFB
a ./PFB/PFBCV/detailinfo
a ./PFB/PFBCV/patinfo
a ./PFB/PFBCV/posting
a ./PFB/PFBEN/detailinfo
a ./PFB/PFBEN/patinfo
a ./PFB/PFBEN/posting
a ./PFB/PFBGH/detailinfo
a ./PFB/PFBGH/patinfo
a ./PFB/PFBGH/posting
a ./PFB/PFBLJ/detailinfo
a ./PFB/PFBLJ/patinfo
a ./PFB/PFBLJ/posting
a ./PFB/PFBME/detailinfo
a ./PFB/PFBME/patinfo
a ./PFB/PFBME/posting
a ./PFB/Upload
a ./PFB/Log
a ./PFB/Archive
a ./Troubleshooting
a ./Log
a ./tst
a ./bin
a ./Archive

nodename:/home/155477> tar tvf /tmp/my-dir.tar
drwxrwxr-x  39 1        0 Aug 07 13:31:34 2010 ./Archive/

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

run the following instead
mkdir /tmp/newdirectory
find . -type d | cpio -pdvmu /tmp/newdirectory
cd /tmp/newdirectory
tar -cf /tmp/my-dir.tar .

There is a way to use cpio to pass the data stream directly to tar, but can't think of it now.
The issue is that find passes text while tar expects something else.
you could also use cpio to create the tar file, the problem you may run into is large names.

find . -type d | cpio -ov --format=tar -O /tmp/my-dir.tar
This command will also do,

$ find . -type d -exec tar cvf dir_struc.tar {} \;

Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

babyb00merAuthor Commented:
While reviewing this question, it occured to me that I might have omitted pertinent information.

The system on which I'm working is running AIX 5.3.

Eventually I found that the following works:

find . -type d | xargs tar cvDf /tmp/test.tar {}

The difference between this command pipe and the original is that I removed the -e and -i options from the xargs command. Also, I've discovered that, according to the man(ual) page for the xargs command, the -e and -i options are obsolete.

The man(ual) page for the tar command describes the -D option as follows:

            Suppress recursive processing when directories are specified.

I've found that this option is not universal - non-existent on HP-UX and assigned to a different function on Solaris. To invoke the same behavior, the GNU version of tar uses "--no-recursion."

Incidentally, "find . -type d -exec tar cvf dir_struc.tar {} \;" did not work, and I never tried the cpio example.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
If I am in some directory called /dir1 and I want to create that same structure in /dir2, but don't want to copy files:

$ mkdir /dir2
$ cd /dir1
$ find . -type d | cpio -pvdm ../dir2
another snippet here:

mkdir /where/you/wantem
cd /source/dir
find * -type d -exec mkdir /where/you/wantem/\{\} \;
babyb00merAuthor Commented:
I had a chance to try some of the other solutions, but none of them worked. I suspect that was due to the idiosyncrasies of the system on which I'm operating. Some of the options were not recognized by the version of cpio and/or tar on my system. Eventually, I found the right combination of options to create an effective pipeline using find, xargs and tar.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.