Copying Folders

There are several different way to copy all the files in one folder to another, while preserving the file permissions and attributes.

Not every method will copy hidden files and some are definitely easier to remember than others…

Using copy

The GNU version of the copy command can copy sub-folders and can preserve file permissions and ownership. It is the easiest to remember, but may is not available in all versions of UNIX/Linux. Using a wildcard to specify the source file names will copy everything from the source folder to the destination folder itself but it will NOT copy any hidden files.

$ cp -rpdv /source/* /dest/

Copy allows you to use relative or full pathnames for the source and destination folders, and the destination pathname may or may not end with a backslash.


 -r    : Copy sub directories.
 -p    : Preserve file attributes.
 -d    : Don't deference (preserves links).
 -v    : Verbose. 

Note – If you omit the wildcard then copy will create a new folder in the destination folder first before copying everything (including hidden files).

Using tar

Should work on almost any UNIX/Linux system, providing there is enough space in /var for any intermediate pipes, and it will copy any hidden files and sub-folders.

$ pushd $(pwd)
$ cd /source/;tar -cpf - . | (cd /dest/; tar xvf -)
$ popd 

Note – You will need to use the full path name for the destination folder name, and the destination folder must exist.

Using rsync

Should work on most Linux systems, but do not expect this to work on all flavors of UNIX. However, it will copy any hidden files and sub-folders.

$ rsync -acP /source/ /dest


 -a    : Archive mode (equivalent to -rlptgoD). 
  -r   : Copy sub directories.  
  -l   : Copy symbolic links as symbolic links.  
  -p   : Preserves file permissions.  
  -t   : Preserves file modification timestamps.  
  -g   : Preserves group.
  -o   : Preserves ownership.
  -D   : Preserves device and special files.

 -c    : Copy files based on their checksum not modification dates.
 -P    : Keep partial file copies and show progress (since rsync performs
         a differential copy keeping any partially copied files on the
         destination should make speed up the subsequent transfer of the
         rest of the file).

Note – There is no backslash on the destination folder name.

This entry was posted in Debian, Linux, Raspbian and tagged . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s