Unix Power ToolsUnix Power ToolsSearch this book

10.12. Copying Directory Trees with cp -r

cp has a -r (recursive) flag, which copies all the files in a directory tree -- that is, all the files in a directory and its subdirectories.

NOTE: One of our Unix systems has a cp without a -r option. But it also has an rcp (Section 1.21) command that does have -r. rcp can copy to any machine, not just remote machines. When I need cp -r on that host, I use rcp -r.

cp -r can be used in two ways. The first is much like normal copies; provide a list of files to copy and an existing directory into which to copy them. The -r option just means that source directories will be copied as well as normal files. The second allows you to copy a single directory to another location.

Some gotchas:

Note that directories can be copied to another machine using the same basic syntax with rcp and scp. The only difference is that remote files have hostname: in front of them; note that remote files can be used either as source or destination. Relative pathnames for remote files are always relative to your home directory on the remote machine.

% scp -r mydata bigserver:backups
% scp -r bass:/export/src/gold-20020131 .

scp and rcp use the same syntax; scp uses SSH (Section 46.6) to do its copying, while rcp uses unencrypted connections.

--DJPH and JP

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.