cp 命令详解

Linux -- cp 复制文件

 

CP(1)                User Commands                CP(1)

 

NAME 名称

    cp - copy files and directories 拷贝文件或目录

 

SYNOPSIS 概要

    cp [OPTION]... [-T] SOURCE DEST

    cp [OPTION]... SOURCE... DIRECTORY

    cp [OPTION]... -t DIRECTORY SOURCE...

 

DESCRIPTION 说明

    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 -dR --preserve=all

        等同于-dR --preserve=all,复制时,保持文件的原有结构和属性。

 

    --attributes-only

        don't copy the file data, just the attributes

        仅拷贝文件属性,不拷贝文件数据。

 

[root@user test]# ll
total 4
-rw-r--r-- 1 root root 1 Mar  5 17:52 test
[root@user test]# cp --attributes-only test test.bk
[root@user test]# ll
total 4
-rw-r--r-- 1 root root 1 Mar  5 17:52 test
-rw-r--r-- 1 root root 0 Mar  5 18:00 test.bk
[root@user test]# 

    --backup[=CONTROL]

        make a backup of each existing destination file

        对存在的目标文件作一备份。

 

    -b

        like --backup but does not accept an argument

        同--backup,对存在的目标文件作一备份,但不接受参数。

 

    --copy-contents

        copy contents of special files when recursive

        在递归时,拷贝特殊文件内容。

 

    -d

        same as --no-dereference --preserve=links

        同--no-dereference --preserve=links,复制符号链接作为符号链接而不是复制它指向的文件。

 

    -f, --force

        if an existing destination file cannot be opened, remove it and try again

        (this option is ignored when the -n option is also used)

        当目标文件存在时,先删除,再复制。(当同-n一起使用时,此参数无效。)

 

    -i, --interactive

        prompt before overwrite (overrides a previous -n option)

        在覆盖前给予提示(将覆盖-n参数)。

 

    -H

        follow command-line symbolic links in SOURCE

        复制符号连接所指向的原始文件,而非符号连接本身。

 

    -l, --link

        hard link files instead of copying

        创建硬连接,而非真正的拷贝。

 

    -L, --dereference

        always follow symbolic links in SOURCE

        复制符号连接所指向的原始文件,而非符号连接本身。

 

    -n, --no-clobber

        do not overwrite an existing file (overrides a previous -i option)

        不覆盖存在的目标文件,即当目标文件存在时,不操作(将覆盖-i参数)。

 

    -P, --no-dereference

        never follow symbolic links in SOURCE

        复制符号连接本身,而非符号连接所指向的原始文件。

 

    -p

        same as --preserve=mode,ownership,timestamps

        同--preserve=mode,ownership,timestamps,保持原文件的权限,所属者,组,时间表属性。

 

    --preserve[=ATTR_LIST]

        preserve the specified attributes (default:  mode,ownership,timestamps),  if possible additional attributes: context, links, xattr, all

        只保护规定的属性(默认:mode,ownership,timestamps),其他属性有:context, links, xattr, all。

 

    -c

        deprecated, same as --preserve=context

        不赞成使用,同--preserve=context。

 

    --no-preserve=ATTR_LIST

        don't preserve the specified attributes

        只保护除此规定属性之外的属性。

 

    --parents

        use full source file name under DIRECTORY

        使用完整的原文件名称作为目标文件名称,当目标位置是目录时有效。

 

    -R, -r, --recursive

        copy directories recursively

        递归复制目标。

 

    --reflink[=WHEN]

        control clone/CoW copies. See below

        控制创建副本方式。

 

    --remove-destination

        remove each existing destination file before attempting to open it (contrast with --force)

        删除每个现有目标文件之前试图打开它(与--force对照)。

 

    --sparse=WHEN

        control creation of sparse files. See below

        控制创建稀疏文件。

 

    --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

        重新指定备份文件后缀。

 

    -t, --target-directory=DIRECTORY

        copy all SOURCE arguments into DIRECTORY

        拷贝所有文件到指定目录。

 

    -T, --no-target-directory

        treat DEST as a normal file

        将目标目录作为普通文件

 

[root@user test]# ls -lR
.:
total 8
drwxr-xr-x 3 root root 4096 Mar  5 17:52 dtest
-rw-r--r-- 1 root root    1 Mar  5 17:52 test

./dtest:
total 4
drwxr-xr-x 2 root root 4096 Mar  5 17:52 test

./dtest/test:
total 0
[root@user test]# cp -T test ./dtest/test 
cp: overwrite './dtest/test'? y
cp: cannot overwrite directory './dtest/test' with non-directory
[root@user test]# cp test ./dtest/test
[root@user test]# ls -lR
.:
total 8
drwxr-xr-x 3 root root 4096 Mar  5 17:52 dtest
-rw-r--r-- 1 root root    1 Mar  5 17:52 test

./dtest:
total 4
drwxr-xr-x 2 root root 4096 Mar  5 17:53 test

./dtest/test:
total 4
-rw-r--r-- 1 root root 1 Mar  5 17:53 test
[root@user test]# 

    -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[=CTX]

        set  SELinux security context of destination file to default type, or to CTX if specified

 

    --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.

    默认情况下,缺省使用--sparse=auto,会自动检测源文件是否为稀疏原文件,以决定目标文件是否为稀疏文件。当指定--sparse=always,会一直创建目标文件为稀疏文件,无论源文件中是否存在足量的0字节序列。使用--sparse=never禁止创建稀疏文件。

 

    When --reflink[=always] is specified, perform a lightweight copy, where the data blocks are copied only when modified. If this is not possible the copy fails, or if --reflink=auto is specified, fall back to a standard copy.

    当指定--reflink或--reflink=always,指定一个轻量级的复制,即coW,当修改时,只复制被修改的数据块。如果避免拷贝失败,或指定--reflink=auto,回退到标准复制。

 

    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:

    除非设置了--suffix选项或SIMPLE_BACKUP_SUFFIX变量,否则使用'~'后缀。版本控制方式由--backup选项决定,或由VERSION_CONTROL环境变量决定。可选方式如下:

 

    none, off

        never make backups (even if --backup is given)

        虽然给定--backup参数,但不生成备份文件,即不作备份

 

    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.

    作为一个特殊案例,当同时给定force和backup,原文件名同目标存在的常规文件的名称相同时,备份原文件

 

    GNU  coreutils  online  help:  <http://www.gnu.org/software/coreutils/> Report cp translation bugs to <http://translationproject.org/team/>

 

AUTHOR

    Written by Torbjorn Granlund, David MacKenzie, and Jim Meyering.

 

COPYRIGHT

    Copyright (C) 2013 Free Software Foundation, Inc.  License GPLv3+: GNU GPL  version 3 or later <http://gnu.org/licenses/gpl.html>.

    This  is  free  software:  you are free to change and redistribute it.  There is NO WARRANTY, to the extent permitted by law.

 

SEE ALSO

    The full documentation for cp is maintained as a Texinfo manual.  If the  info  and cp programs are properly installed at your site, the command

        info coreutils 'cp invocation'

    should give you access to the complete manual.

    cp 的完整文档是以Texinfo手册形式维护的。如果info和cp程序都已经安装,那么执行命令:

        info coreutils 'cp invocation'

    应该会让你访问到整篇手册。

 

GNU coreutils 8.22              June 2014                CP(1)

猜你喜欢

转载自lshj.iteye.com/blog/2360371