In the Finder, you select files to copy by clicking and Option-dragging them or by choosing File > Duplicate.
In Unix, you select files to copy by typing their names and/or paths as arguments to the cp command ( cp for copy ). You can use wildcards (see Chapter 2), and the files need not all start in the same directory.
The behavior of the cp command depends on its arguments. It will behave one way if its last argument is a directory and another way if its last argument is a file. The cp command produces no output unless it encounters an error.
To copy a file in the same directory:
- cp oldfilename newfilename
If newfilename is an existing nondirectory file, then cp will attempt to overwrite the file, replacing its contents.
To copy a file into a directory:
- cp oldfilename directorypath
If the last argument to cp is a directory, then the file is copied into the directory and the copy has the same name as the original.
cp /etc/appletalk.cfg .
creates a copy of /etc/appletalk.cfg in the current directory (represented by . ), and the copy is also named appeltalk.cfg.
To copy a file from one directory into another and change its name:
- cp oldfilename directory/newname
Simply make sure that the last argument to cp is a path to a file, not to a directory. For example:
cp /etc/appletak.cfg /tmp/atalk.copy
To copy more than one file at a time:
- cp file1 file2 directorypath
cp script.pl config.pl bin/
When cp is used with more than two arguments, it assumes that the last argument is a directory (you get an error message if it is not). All the files are then copied into the directory.
The source files need not all be in the same directory. The following example would copy all the .mp3 files from the /tmp and Public/ directories into the Music/ directory:
cp /tmp/*.mp3 Public/*.mp3 Music/
To copy an entire directory:
- cp -R directory destination
cp -R /etc/httpd .
copies the entire /etc/httpd directory into the current directory.
With the -R option, cp recursively copies directories named in its arguments. The destination (last argument) must be a directoryoften it will be simply . for current directory .
cp -R file1 dir1 dir2 dir3
copies file1 and all of dir1 and dir2 into dir3 .
Add the -p option to tell cp to attempt to preserve the file-modification times and permissions from the source file(s). For example,
cp -Rp /etc/httpd .
We say "attempt" because some information about the files may not be preserved unless you execute the command as root, using the sudo command described in Chapter 11. Without the -p option, when cp overwrites an existing file, it will still preserve some information about the overwritten file, such as its modification time.
To copy (only) the contents of an entire directory:
Old Mac Files in the Unix World
In Mac OS 9 and earlier, files saved on a Mac actually consisted of either one or two parts , or forks . The data fork was always present; this was where the file data was stored. The resource fork contained icons and other resources associated with the file, such as which application to use when opening it. When these two-part files were copied to non-Macintosh systems, the resource fork was lost unless special steps were taken, such as encoding them into a single binhex file.
On all non-Darwin versions of Unix, and on Mac OS X before version 10.4, the standard Unix tools for manipulating files do not preserve the resource fork on an old-style Mac file. So if you copy a file using cp on one of these systems, the resource fork will be missing from the copy.
Starting with Mac OS X 10.4, the cp and mv commands will recognize and preserve resource forks. In versions of Mac OS X before 10.4, you can use the two commands /Developer/Tools/CpMac and /Developer/Tools/MvMac instead. These commands do not have all the options of the standard cp and mv commands. See man CpMac and man MvMac for details.