Copying Files

One of the workhorse Linux file-manipulation commands is cp, which copies files. The syntax for this command is deceptively simple:

cp [options] source destination

In this case, source is one or more source files, possibly expressed using wildcard operators. The destination can be either a file, in which case the original is copied to the new filename; or a directory, in which case the original is copied to the new directory using its original filename. If you specify multiple source files, either explicitly or via a wildcard, the destination must be a directory.

The complexity of cp emerges in the use of its many options, some of which are summarized in Table 5.2. This table isn't comprehensive; consult the cp man page for more options.

Table 5.2: Common cp Options

Option

Effect

-a

Archive a directory; copy recursively (as in -r) and preserve ownership, permissions, and symbolic link status.

-b

Make a backup of any destination file the operation replaces.

-f or-force

Replace any matching destination file without asking for confirmation.

-i or-interactive

Query before replacing any existing destination file.

-1 or—link

Create hard links rather than copies.

-P

Preserve ownership, permissions, and time stamps.

-r, -R, or-recursive

Copy directories recursively.

-s or-symbolic-link

Create symbolic links rather than copies.

-u or-update

Copy only when the destination file doesn't exist or is older than the source file.

-x or-one-file-system

Don't recurse into mounted filesystems (used in conjunction with -r)

0 0

Post a comment