File copying is about more than just content – the metadata for user ownership, permissions, and timestamps is often critical to retrieval and function.
Below are the relevant switches for metadata preservation when using cp, rsync, and tar. These typically need sudo in order to work.
cp
copy with recursion (-r) and (-p) preserve mode,ownership,timestamps
sudo cp -rp <sourceDir> <destDir>
rsync
(-a) archive recursive which preserves permissions/time/ownership, (-u) skip files that are newer on the receiver, (-v) verbose, (-h) numbers human readable.
sudo rsync -auvh --progress <sourceDir>/ <destDir> # remote rsync with ssh key rsync -e "ssh -i ~/.ssh/sshprivkey" -auvh --progress <sourceDir>/ <remoteHost>:<remoteBaseDir>
If you specify both sourceDir and destDir with an ending slash, the destination will be at an extra level of subfolder. Avoid this by suffixing only the source with an ending slash, e.g. “<sourceDir>/ <baseDir>”.
tar
preserve permission (-p) verbose (-v)
tar cvfp <myarchive>.tar -C <sourceDir>/ . # make sure directory you are extracting to pre-exists # use sudo to allow setting permissions upon extraction mkdir -p <destDir> sudo tar xvfp <myarchive>.tar -C <destDir>
When tar’ing with -C the order is important, make sure the “-C” comes first, then the final period.
Validation
If you want to run a test, you can use a test script on my github page that creates a directory called ‘testp’, that has owners named bugs, daffy, and michael. It assigns different levels of permission, ownership, and modifications dates.
wget https://raw.githubusercontent.com/fabianlee/blogcode/master/permissiontest/testpermissions.sh chmod ug+r+x testpermissions.sh ./testpermissions.sh
This creates a directory named ‘testp’, you can view the owner, permissions, and modifications using ‘ls -l testp/*’
Test each utility described above by typing these commands.
sudo cp -rp testp testcp sudo rsync -auvh --progress testp/* testrsync tar cvfp myarchive.tar -C testp/ . mkdir testtar && tar xvfp myarchive.tar -C testtar
You can verify that the ownership, permissions, and modifications dates have been preserved by issuing the commands below and noting they all have the same metadata.
ls -l testp/* ls -l testcp/* ls -l testrsync/* ls -l testtar/*
REFERENCES
https://linux.die.net/man/1/cp (man cp)
https://linux.die.net/man/1/rsync (man rsync)
https://linux.die.net/man/1/tar (man tar)
https://unix.stackexchange.com/questions/65077/is-it-possible-to-see-cp-speed-and-percent-copied
NOTES
rsync dry run (-a) archive recursive (-v) verbose, (-u) skip files newer (-n) dry run
$ sudo rsync -aunv sourceDirectory/ destinationDirectory/