From ArchWiki
Revision as of 20:55, 13 January 2009 by Yingwuzhao (talk | contribs)
Jump to navigation Jump to search

Copying large amounts of files usually works fine with the 'cp' command but if there are any symbolic links in the files being copied you will copy the files being linked to instead of the links themselves. To solve this one can use the tar command.

Also,copying a directory tree and its contents to another filesystem using tar can preserve ownership, permissions, and timestamps. A neat trick allows using tar to perform a recursive copy without creating an intermediate tar file.

To copy all of the files and subdirectories in the current working directory to the directory /target, use:

tar cf - /Directory | ( cd /Destination; tar xfp -)

The first part of the command before the pipe instruct tar to create an archive of everything in the /Directory and write it to standard output (the - in place of a filename frequently indicates stdout). The commands within parentheses cause the shell to change directory to the Destination directory and untar data from standard input. Since the cd and tar commands are contained within parentheses, their actions are performed together.

The -p option in the tar extraction command directs tar to preserve permission and ownership information, if possible given the user executing the command. If you are running the command as superuser, this option is turned on by default and can be omitted.