You are on page 1of 1

Chapter 20: Directory Copying Copying files from one place to another seems a trivial task hardly worth

mentio ning in an advanced shell-scripting book. However, copying groups of files with the typical cp command doesn't result in a true copy. You might expect an exact duplicate of the source files, but there may be soft links, hard links, subdirec tories, pipes, dot files, and regular files, among others, and the cp command do esn't work as you might expect with all of them. You need to make a few tweaks t o get a copy command that performs well for all file and link types. For testing purposes, I created a directory that contains some of each of these file types that can be used to check whether the copy has been performed correctly. Using cp The following is the cp command that comes the closest to duplicating the test d irectory: cp -Rp * /dest/dir The -R option tells cp to recurse through the directory structure it is copying; the -p option preserves permissions, ownership, and access and modification tim es of the original files. The copy is based on the access rights of the user per forming the copy. However, the actual functionality of the cp command falls short of expectations. Symbolic links in the destination directory are created with the modification t ime noting when the copy was performed, not when the original files were created , although this shouldn't be a significant issue since the actual files that are linked keep their original modification time. The main issue with the cp comman d is that hard links are not maintained. Hard links are copied as individual fil es; they are not treated as links to the same file. This may result in a signifi cant storage issue if you have many hard links whose copies no longer conserve d isk space as duplicate files. Newer versions of the cp command have an -a switch. This option preserves as man y source-file attributes as possible, including hard links. cp -a * /dest/dir In its application memory, the cp command keeps track of files that contain a li nk count greater than one. This works fine for relatively small copies, but has the potential downside that during execution the process could run out of memory and fail because of an excessive number of hard links that need caching.

You might also like