Operating System - HP-UX
1819736 Members
2848 Online
109606 Solutions
New Discussion юеВ

Copying one directory to another. tar?

 
SOLVED
Go to solution
dictum9
Super Advisor

Copying one directory to another. tar?

I have /dir1/subdir1 which I need copied to /dir2/subdir2

I am not going to use the cp command, because the last time I used it on some of the key partitions, I hosed the system.... it may not preserve links and dot files... etc. now how about tar.

I know I can create a tar ball with
tar -cvf dir1.tar /dir1/subdir1

but how do I untar it, i.e. where do I put it?

If i put in /dir2/subdir2, it will create

/dir2/subdir2/dir1/subdir1 tree.. which is not good.

Ideas?
11 REPLIES 11
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Copying one directory to another. tar?

The problem is that you are using absolute paths.

cd to desired starting directory, e.g.
cd /dir1
tar cvf /var/tmp/dir1.tar . # this will get everything from the CWD down and the paths will be relative

Now cd to the directory above the desired destination, e.g
cd /xxx/yyy
mkdir dir2
chown me:mygroup dir2 # or whoever
chmod 750 dir2 # or whatever
cd dir2
tar xvf /var/tmp/dir1.tar

If it ain't broke, I can fix that.
V.Manoharan
Valued Contributor

Re: Copying one directory to another. tar?

Hi,
if it is not a live system directory.
1. take tar archive backup
2. rename /dir1 & /dir1/subdir1 to /dir2 & /dir2/subdir2
3. extract the tar archive.

#tar -cvf dir1.tar /dir1/subdir1
#mv /dir1 /dir2
#mv /dir2/subdir1 /dir2/subdir2
#tar -xvf dir1.tar

regards
V.Manoharan
IT_2007
Honored Contributor

Re: Copying one directory to another. tar?

you can specify both in one command.

tar cvf dir1.tar |(cd /dest; tar -xvf .)

James R. Ferguson
Acclaimed Contributor

Re: Copying one directory to another. tar?

Hi:

This uses 'fbackup' and 'frecover' and hence handles largefiles *and* does not inflate sparse files like 'cp' would:

# cd srcdir && fbackup -i . -f - | ( cd dstdir && frecover -Xsrf - )

Regards!

...JRF...
Kofi ARTHIABAH
Honored Contributor

Re: Copying one directory to another. tar?

I think IT_2007's post should be:

tar -cvf - /dir1/subdir1/ | (cd /dir2/subdir2 ; tar -xvf - )

Hope this helps.

Kofi
nothing wrong with me that a few lines of code cannot fix!
Sandman!
Honored Contributor

Re: Copying one directory to another. tar?

mkdir /newdir
cd /olddir
tar cf - * | (cd /newdir; tar xvf - )
Kofi ARTHIABAH
Honored Contributor

Re: Copying one directory to another. tar?

I think IT_2007's post should be:

tar -cvf - ./dir1/subdir1/ | (cd ./dir2/subdir2 ; tar -xvf - )

Hope this helps.

Kofi
nothing wrong with me that a few lines of code cannot fix!
Sp4admin
Trusted Contributor

Re: Copying one directory to another. tar?

Hi etc,

cd into the directory. The use the tar command to create a tar ball.

create tar ball
#tar -cvf dir.TAR dir

Mv or copy tar file to directory
# mv dir.TAR /dir2

Untar the tar ball
#cd /dir2
#tar -xvf dir.TAR

sp,

Bill Hassell
Honored Contributor

Re: Copying one directory to another. tar?

tar is not very easy to work around it's design. You'll find that cpio is a bit faster but the fbackup/frecover method is by far the fastest method and the only one you should use if you have large files (bigger than 2Gb). tar and cpio have very limited file size capability:

mkdir /some_new_dir
cd /old_dir
find . | cpio -pudlmv /some_new_dir


Bill Hassell, sysadmin
Steven E. Protter
Exalted Contributor

Re: Copying one directory to another. tar?

Shalom etc

If the users exist on both systems:

scp -rp /dir2/subdir2/* system2:/dir2/subdir2

I suppose you are using tar to preserve the links.

Are they soft or hard links?

tar -cvf dir1.tar /dir1/subdir1
tar -rvf dir1.tar /dir2/subdir2

if it must be one tar file.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
dictum9
Super Advisor

Re: Copying one directory to another. tar?

I used A. Clay Stephenson's suggestion, and it worked nicely... I wasn't sure about tar's absolute vs. relative paths.


I will save these cool one-liners for the next time..