|
该命令的功能是将给出的文件或目录拷贝到另一文件或目录中,同MSDOS下的copy命令一样,功能十分强大。
语法:cp [选项] 源文件或目录 目标文件或目录
说明:该命令把指定的源文件复制到目标文件或把多个源文件复制到目标目录中。
该命令的各选项含义如下:
- a 该选项通常在拷贝目录时使用。它保留链接、文件属性,并递归地拷贝目录,其作用等于dpR选项的组合。
- d 拷贝时保留链接。
- f 删除已经存在的目标文件而不提示。
- i 和f选项相反,在覆盖目标文件之前将给出提示要求用户确认。回答y时目标文件将被覆盖,是交互式拷贝。
- p 此时cp除复制源文件的内容外,还将把其修改时间和访问权限也复制到新文件中。
- r 若给出的源文件是一目录文件,此时cp将递归复制该目录下所有的子目录和文件。此时目标文件必须为一个目录名。
- l 不作拷贝,只是链接文件。
需要说明的是,为防止用户在不经意的情况下用cp命令破坏另一个文件,如用户指定的目标文件名已存在,用cp命令拷贝文件后,这个文件就会被新源文件覆盖,因此,建议用户在使用cp命令拷贝文件时,最好使用i选项。
例子:
cp -i file1 file2 (将文档 file1 复制成 file2 . –i为提示确认。)
cp file1 dir1 将文档 file1 复制到目录 dir1 下,文件名仍为 file1.
cp /tmp/file1 . 将目录 /tmp 下的文档 file1复制到现行目录下,档名仍为 file1.
cp /tmp/file1 file2 将目录 /tmp 下的文档 file1现行目录下,档名为file2
cp -r dir1 dir2 (recursive copy) 复制整个目录.若目录 dir2 不存在,则将目录dir1,及其所有文档和子目录,复制到目录 dir2 下,新目录名称为dir1.若目录dir2不存在,则将dir1,及其所有文档和子目录,复制为目录 dir2
Linux中对cp命令的帮助输出:
[root@DevDBServer]# cp --help
Usage: cp [OPTION]... SOURCE DEST
or: cp [OPTION]... SOURCE... DIRECTORY
or: cp [OPTION]... --target-directory=DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
-a, --archive same as -dpR
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
--copy-contents copy contents of special files when recursive
-d same as --no-dereference --preserve=link
--no-dereference never follow symbolic links
-f, --force if an existing destination file cannot be
opened, remove it and try again
-i, --interactive prompt before overwrite
-H follow command-line symbolic links
-l, --link link files instead of copying
-L, --dereference always follow symbolic links
-p same as --preserve=mode,ownership,timestamps
--preserve[=ATTR_LIST] preserve the specified attributes (default:
mode,ownership,timestamps), if possible
additional attributes: links, all
-c same as --preserve=context
--no-preserve=ATTR_LIST don't preserve the specified attributes
--parents append source path to DIRECTORY
-P same as `--no-dereference'
-R, -r, --recursive copy directories recursively
--remove-destination remove each existing destination file before
attempting to open it (contrast with --force)
--reply={yes,no,query} specify how to handle the prompt about an
existing destination file
--sparse=WHEN control creation of sparse files
--strip-trailing-slashes remove any trailing slashes from each SOURCE
argument
-s, --symbolic-link make symbolic links instead of copying
-S, --suffix=SUFFIX override the usual backup suffix
--target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY
-u, --update copy only when the SOURCE file is newer
than the destination file or when the
destination file is missing
-v, --verbose explain what is being done
-x, --one-file-system stay on this file system
-Z, --context=CONTEXT set security context of copy to CONTEXT
--help display this help and exit
--version output version information and exit
By default, sparse SOURCE files are detected by a crude heuristic and the
corresponding DEST file is made sparse as well. That is the behavior
selected by --sparse=auto. Specify --sparse=always to create a sparse DEST
file whenever the SOURCE file contains a long enough sequence of zero bytes.
Use --sparse=never to inhibit creation of sparse files.
The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable. Here are the values:
none, off never make backups (even if --backup is given)
numbered, t make numbered backups
existing, nil numbered if numbered backups exist, simple otherwise
simple, never always make simple backups
As a special case, cp makes a backup of SOURCE when the force and backup
options are given and SOURCE and DEST are the same name for an existing,
regular file.
|
|
|