Next: , Up: Recipes   [Contents][Index]


B.1 Copying directory hierarchies

This is a traditional way to copy a directory hierarchy preserving the dates, modes, owners and link-structure of all the files therein. It was used back when the cp command lacked the -a option:

$ (cd sourcedir; tar -cf - .) | (cd targetdir; tar -xf -)

You can avoid subshells by using -C option:

$ tar -C sourcedir -cf - . | tar -C targetdir -xf -

The same command using long option forms:

$ (cd sourcedir; tar --create --file=- . ) \
       | (cd targetdir; tar --extract --file=-)

or

$ tar --directory sourcedir --create --file=- . \
       | tar --directory targetdir --extract --file=-