Author Topic: Copy A Directory Easily  (Read 986 times)

Offline scotbuff

  • Sys Admin
  • UNIX User
  • *****
  • Posts: 174
  • Karma: +2/-0
    • View Profile
    • Scott.Buffington.me
Copy A Directory Easily
« on: December 16, 2005, 01:08:41 pm »
If you want to copy everything from one directory into a new directory (the directory must exist) you can use this command.

(cd /home/sbuffing/scripts; tar cf - .) | (cd /home/sbuffing/newscripts; tar xf -)

Very handy.

cp -R is generally unsafe to copy directories with since it will copy through symbolic links rather than just copying the link (newer cp's have an -H flag that will work around this).
« Last Edit: December 23, 2005, 08:30:28 pm by scotbuff »

Offline scotbuff

  • Sys Admin
  • UNIX User
  • *****
  • Posts: 174
  • Karma: +2/-0
    • View Profile
    • Scott.Buffington.me
Copy A Directory Easily - Server to Server
« Reply #1 on: March 20, 2006, 11:25:56 am »
You can use tar, | and ssh to copy a directory on one server to a directory on another server.

tar cpf - /home/connect|(ssh username@hostname "cd /home/connect; tar xpf -")

You may need to prefix sudo to the tar command on the remote server depending on permissions.