2008-01-16

UNIX tip of the day: preserve attributes by copying with tar

This quick-n-dirty command-line, if run as root, will retain all file permissions, timestamps, and ownership data while doing a recursive copy of a directory. Linux (specifically, the GNU version of cp) has cp -a which does about the same thing as this. Under most BSD Flavors, cp -Rp does the same thing as well. That said, AIX, HP/UX and Solaris need love too. This works on any POSIX platform and comes in very handy in a pinch.

# cd source-dir ; tar cf - . | (cd destination-dir && tar xBf - )

Adjustments: You should be able make the last tar command "tar xvBf -" if you wish to see the filenames as they're being copied, without any problems.


This command should be run as root, or at least both tar commands run with sudo, otherwise tar may not have the permissions needed to read all of the files, or set all the permissions needed at the destination directory. 

blog comments powered by Disqus