Copy entire contents of a directory and preserve permissions

Contributor Icon Contributed by nifran  
Tag Icon Tagged: Solaris  

Sometimes it’s needed to move the contents of a directory to another mount point, or even another system using tar.

tar preserves permissions on files better than other methods of copying.


To move on the same system, do the following:

cd dir1 && tar -cf – . | (cd dir2 && tar -xpvf -)

dir1 is the directory you want to copy
dir 2 is the directory you want the copy to go into.

This tar’s the current directory to STDOUT, then changes directory and untar’s the archive without ever having to find the space for a .tar file.

You can also do this across systems via ssh:
cd dir1 && tar -cf – . | ssh system2 “cd dir2 && tar -xpvf -”

 

3 Comments -


  1. trwww said on January 1, 2010

    Your command copy / pastes from FF 3.5 to MindTerm on windows XP as:

    cd dir1 && tar -cf ? . | (cd dir2 && tar -xpvf -)

    Note the ? instead of the -

    I was in my user’s home directory at the time so its all gone now! Whee!

  2. shreyash said on January 3, 2010

    nice work dude……i like all solaris recipes…….i appreciate ur work……

  3. Telmerobert said on December 23, 2010

    I have a windows 2008 server machine i accessed my solaris machine using telnet .
    i would like to copy or transfer some of files from windows server 2008 machine please help me.

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -