< Previous | Contents | Next >
Useful Options And Examples
Here are some of the commonly used options (the short option and the equivalent long option) for cp:
Table 4-4: cp Options
Option Meaning
Option Meaning
-a, --archive Copy the files and directories and all of their attributes,
including ownerships and permissions. Normally, copies take on the default attributes of the user performing the copy.
-i, --interactive Before overwriting an existing file, prompt the user for
confirmation. If this option is not specified, cp will silently overwrite files.
-r, --recursive Recursively copy directories and their contents. This
option (or the -a option) is required when copying directories.
-u, --update When copying files from one directory to another, only
copy files that either don't exist, or are newer than the existing corresponding files, in the destination directory. This is useful when copying large numbers of file as it skips over files that don't need to be copied.
-v, --verbose Display informative messages as the copy is
performed.
Table 4-5: cp Examples
Command Results
Command Results
cp file1 file2 Copy file1 to file2. If file2 exists, it is overwritten
with the contents of file1. If file2 does not exist, it is created.
cp -i file1 file2 Same as above, except that if file2 exists, the user is
prompted before it is overwritten.
cp file1 file2 dir1 Copy file1 and file2 into directory dir1. dir1 must already exist.
cp dir1/* dir2 Using a wildcard, all the files in dir1 are copied
into dir2. dir2 must already exist.
cp -r dir1 dir2 Copy the contents of directory dir1 to directory
dir2. If directory dir2 does not exist, it is created and, after the copy, will contain the same contents as directory dir1.
If directory dir2 does exist, then directory dir1 (and its contents) will be copied into dir2.