How to copy files without losing file/folder permissions?

Most versions of Linux comes with a great compression utility called tar. Sometimes you need to move huge directories from one host to another. It is a tediuous job specially when you need to preserve file permissions and settings. I use following steps to compress and move files between different servers.

  1. Go to the source computer:
  2. Execute: cd /path/to/folder/to/copy
  3. Execute: tar cvpzf put_your_name_here.tar.gz .
  4. Copy put_your_name_here.tar.gz to the other server using scp or a UI tool like Transmit or CuteFTP.
  5. Go to the destination computer:
  6. Execute: cd /path/to/destination/folder
  7. Execute: tar xpvzf put_your_name_here.tar.gz


tar
will recreate the archived folder structure with all permissions intact.

Those commands will archive the contents of the source folder and then extract them into the destination folder. If you want to copy the folder itself, then you should, at step 2:

  1. cd /path/to/parent/folder
  2. tar cvpzf put_your_name_here.tar.gz folder_to_copy

The same mechanism could be used for single files.

 

If you can connect from one computer to the other using ssh, (rsync) might be more practical.

What is Tar?

In computing, tar is a computer software utility for collecting many files into one archive file, often referred to as a tarball, for distribution or backup purposes.  GNU Tar provides the ability to create tar archives, as well as various other kinds of manipulation. For example, you can use Tar on previously created archives to extract files, to store additional files, or to update or list files which were already stored. The name is derived from “tape archive”, as it was originally developed to write data to sequential I/O devices with no file system of their own.