Gregg's MOTD

Tips & Tricks that I've Encountered Over the Years...

Copying a Directory Tree Recursively Using tar

September 22, 2023 — Gregg Szumowski

You can use tar to copy a directory tree to another directory in one shot along with preserving ownership, permissions and timestamps. This also avoids making an intermediary tarfile which may cause problems if the size of the file copy is large and the storage resources are low. Just cd to the top of the directory that you want to copy and begin.

Let’s assume that you want to copy the contents of the source directory to a target directory:

$ cd /path/to/source
$ tar cf - * | (cd /target; tar xfp - )

Tags: cli, tar, copying, motd