Copies Files Commands in Linux
cp [options] source-file destination-file
cp [options] source-file-list destination-directory
The cp utility copies one or more files. It can either make a copy of a
single file (first format) or copy
one or more files to a directory (second format). With the –R option, cp
can copy directory hierarchies.
Arguments: The source-file is the pathname of the file that
cp makes a copy of. The destinationfile
is the pathname that cp assigns to the resulting copy of the file.
The source-file-list is a list of one or more pathnames of files that cp
makes copies
of. The destination-directory is the pathname of the directory in which
cp places
the copied files. With this format, cp gives each copied file the same
simple filename
as its source-file.
The –R option enables cp to copy directory hierarchies recursively from
the sourcefile-
list into the destination-directory.
Options: Under Linux, cp accepts the common options
described on page 603. Options preceded
by a double hyphen (––) work under Linux only. Except as noted, options
named with
a single letter and preceded by a single hyphen work under Linux and OS
X.
––archive –a Attempts to preserve the owner, group,
permissions, access date, and modification date of source file(s) while copying
recursively without dereferencing symbolic links. Same as –dpR.
––backup –b If copying a file would remove or overwrite an existing
file, this option makes
a backup copy of the file that would be overwritten. The backup copy has
the same name as the destination-file with a tilde (~) appended to it. When you
use both ––backup and ––force, cp makes a backup copy when you try to copy a
file over itself. For more backup options, search for Backup options in the
core utils info page.
–d For each file that is a
symbolic link, copies the symbolic link, not the file the link points to. Also
preserves hard links in destination-files that exist between corresponding
source-files. This option is equivalent to ––no-dereference and
––preserve=links.
––force – f When the
destination-file exists and cannot be opened for writing, causes cp to try to
remove destination-file before copying source-file. This option is useful when
the user copying a file does not have write permission to an existing destination-file
but does have write permission to the directory containing the destination-file.
Use this option with –b to back up a destination file before removing or
overwriting it.
–H (partial dereference) For each file that is a symbolic link, copies
the file the link points to, not the symbolic link itself. This option affects
files specified on the command line; it does not affect files found while
descending a directory hierarchy.
This option treats files that are not symbolic links normally. Under OS
X works with –R only. See page 623 for an example of the use of the –H versus
–L options.
––interactive –i Prompts you whenever cp would overwrite a file. If you
respond with a string that starts with y or Y, cp copies the file. If you enter
anything else, cp does not copy the file.
––dereference –L (dereference) For each file that is a symbolic link,
copies the file the link points to, not the symbolic link itself. This option
affects all files and treats files that are not symbolic links normally. Under
OS X works with –R only.
––no-dereference –P (no dereference) For each file that is a symbolic
link, copies the symbolic link, not the file the link points to. This option
affects all files and treats files that are not symbolic links normally. Under
OS X works with –R only.
––preserve[=attr] –p Creates a destination-file with the same owner,
group, permissions, access date, and modification date as the source-file. The
–p option does not take an argument.
Without attr, ––preserve works as described above. The attr is a
commaseparated
list that can include mode (permissions and ACLs), ownership (owner and
group), timestamps (access and modification dates), links (hard links), and all (all attributes).
––parents Copies a relative
pathname to a directory, creating directories as needed. See the “Examples”
section.
––recursive –R or –r
Recursively copies directory hierarchies including ordinary files. Under
Linux, the ––no-dereference (–d) option is implied: With the –R, –r, or
––recursive option, cp copies the links (not the files the links point to). The
–r and ––recursive options are available under Linux only.
––update –u Copies only when the destination-file does not exist or when
it is older
than the source-file (i.e., this option will not overwrite a newer
destination file).
––verbose –v Displays the name of each file as cp copies it.
Examples
The first command makes a copy of the file letter in the working
directory. The name of the copy is letter.sav.
$ cp letter letter.sav
The next command copies all files with filenames ending in .c into the
archives directory, which is a subdirectory of the working directory. Each
copied file retains its simple filename but has a new absolute pathname.
The –p (––preserve) option causes
the copied files in archives to have the same owner, group, permissions, access
date, and modification date as the source files.
$ cp -p *.c archives.
The next example copies memo from Sam’s home directory to the working
directory:
$ cp ~sam/memo .
The next example runs under Linux and uses the ––parents option to copy
the file memo/thursday/max to the dir directory as dir/memo/thursday/max. The
find utility shows the newly created directory hierarchy.
$ cp --parents memo/thursday/max dir
$ find dir
dir
dir/memo
dir/memo/thursday
dir/memo/thursday/max
The following command copies the files named memo and letter into
another directory.
The copies have the same simple filenames as the source files (memo and
letter) but have different absolute pathnames. The absolute pathnames of the
copied files are /home/sam/memo and /home/sam/letter, respectively.
$ cp memo letter /home/sam
The final command demonstrates one use of the –f (––force) option. Max
owns the working directory and tries unsuccessfully to copy one over another
file (me) that he does not have write permission for. Because he has write
permission to the directory that holds me, Max can remove the file but cannot
write to it.
The –f (––force) option unlinks, or removes, me and then copies one to
the new file named me.
$ ls -ld
drwxrwxr-x 2 max max 4096 Oct 21 22:55 .
$ ls -l
-rw-r--r-- 1 root root 3555 Oct 21 22:54 me
-rw-rw-r-- 1 max max 1222 Oct 21 22:55 one
$ cp one me
cp: cannot create regular file 'me': Permission denied
$ cp -f one me
$ ls -l
-rw-r--r-- 1 max max 1222 Oct 21 22:58 me
-rw-rw-r-- 1 max max 1222 Oct 21 22:55 one
If Max had used the –b (––backup) option in addition to –f (––force), cp
would have created a backup of me named me~.
No comments:
Post a Comment