Copying directories with tar

This is one of those, “can’t seem to memorize something” posts… Sometimes copying files with cp is just painful. Instead:

[root@sparky ~]# mkdir /tmp/newopt && mount /dev/mapper/system-opt /tmp/newopt
[root@sparky ~]# ( cd /opt && tar --xattrs -cf - . ) | ( cd /tmp/newopt && tar xvfp - )

This will copy files and directories from /opt to /newopt, including extended attributes like acl’s and SELinux contexts. And it’s faster than using cp as well…