Linux cp命令文档(cp指令)(cp --help)(各选项参数用法解释)

Linux cp命令文档(cp指令)(cp --help)(各选项参数用法解释)

文章目录

cp命令文档英文中文

关键点示例`-x, --one-file-system`(保持在此文件系统上,不做跨域文件系统拷贝)含义示例

疑问什么是“长选项的强制参数对短选项也是强制的”?什么是“稀疏文件”?

cp命令文档

英文

root@ubuntu:~# cp --help

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

or: cp [OPTION]... SOURCE... DIRECTORY

or: cp [OPTION]... -t 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 -dR --preserve=all

--attributes-only don't copy the file data, just the attributes

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

-i, --interactive prompt before overwrite (overrides a previous -n

option)

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

-P, --no-dereference never follow symbolic links in SOURCE

-p same as --preserve=mode,ownership,timestamps

--preserve[=ATTR_LIST] preserve the specified attributes (default:

mode,ownership,timestamps), if possible

additional attributes: context, links, xattr,

all

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

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

-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 set SELinux security context of destination

file to default type

--context[=CTX] like -Z, or if CTX is specified then set the

SELinux or SMACK security context to CTX

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

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.

Use --reflink=never to ensure a standard copy is performed.

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.

GNU coreutils online help:

Full documentation at:

or available locally via: info '(coreutils) cp invocation'

中文

root@ubuntu:~# cp --help

使用方法: cp [选项]... [-T] 源 目标

或者: cp [选项]... 源... 目录

或者: cp [选项]... -t 目录 源...

将源复制到目标,或将多个源复制到目录。

长选项的强制参数对短选项也是强制的。

-a, --archive 等同于 -dR --preserve=all

--attributes-only 不复制文件数据,只复制属性

--backup[=CONTROL] 对每个已存在的目标文件进行备份

-b 类似 --backup,但不接受参数

--copy-contents 在递归时复制特殊文件的内容

-d 等同于 --no-dereference --preserve=links

-f, --force 如果无法打开已存在的目标文件,则删除它并重试(当-n选项也被使用时,此选项会被忽略)

-i, --interactive 覆盖前提示(覆盖之前的 -n选项)

-H 在源中跟随命令行符号链接

-l, --link 硬链接文件,而不是复制

-L, --dereference 总是在源中跟随符号链接

-n, --no-clobber 不覆盖已存在的文件(覆盖之前的 -i选项)

-P, --no-dereference 在源中永不跟随符号链接

-p 等同于 --preserve=mode,ownership,timestamps

--preserve[=ATTR_LIST] 尽可能保留指定的属性(默认:mode,ownership,timestamps),可能包括其他属性:context, links, xattr, all

--no-preserve=ATTR_LIST 不保留指定的属性

--parents 在目录下使用完整的源文件名

-R, -r, --recursive 递归复制目录

--reflink[=WHEN] 控制克隆/CoW副本。参见下文

--remove-destination 在试图打开它之前删除每个已存在的目标文件(与 --force形成对比)

--sparse=WHEN 控制创建稀疏文件。参见下文

--strip-trailing-slashes 从每个源参数中移除任何尾部斜杠

-s, --symbolic-link 创建符号链接,而非复制

-S, --suffix=SUFFIX 覆盖通常的备份后缀

-t, --target-directory=DIRECTORY 将所有源参数复制到目录

-T, --no-target-directory 将目标视为普通文件

-u, --update 仅当源文件比目标文件新或者目标文件缺失时进行复制

-v, --verbose 解释正在执行的操作

-x, --one-file-system 保持在此文件系统上

-Z 将目标文件的SELinux安全上下文设置为默认类型

--context[=CTX] 类似于 -Z,或者如果指定了CTX,则将SELinux或SMACK安全上下文设置为CTX

--help 显示此帮助并退出

--version 输出版本信息并退出

默认情况下,通过粗略的启发式检测到稀疏源文件,并使相应的目标文件变得稀疏。这是由 --sparse=auto选择的行为。指定 --sparse=always以在源文件包含足够长的零字节序列时创建一个稀疏的目标文件。使用 --sparse=never来禁止创建稀疏文件。

当指定 --reflink[=always]时,执行轻量级复制,其中数据块仅在修改时复制。如果这不可能,复制失败,或者如果指定了 --reflink=auto,则回退到标准复制。使用 --reflink=never确保执行标准复制。

备份后缀是'~',除非使用 --suffix或SIMPLE_BACKUP_SUFFIX进行设置。可以通过 --backup选项或VERSION_CONTROL环境变量选择版本控制方法。以下是可选值:

none, off 从不进行备份(即使给出了 --backup)

numbered, t 进行编号备份

existing, nil 如果编号备份存在,则编号,否则简单

simple, never 总是进行简单备份

作为特例,当给出强制和备份选项并且源和目标对于已存在的常规文件有相同的名称时,cp会备份源。

GNU coreutils在线帮助:

完整文档在:

或本地可通过:info '(coreutils) cp invocation' 获取

关键点示例

-x, --one-file-system(保持在此文件系统上,不做跨域文件系统拷贝)

选项 -x 或 --one-file-system 在使用 cp 命令时有特定的作用,它告诉 cp 在复制过程中避免跨越文件系统边界。

含义

当指定 -x 或 --one-file-system 选项时,如果 cp 命令碰到挂载点(即一个文件系统挂载在另一个文件系统上的目录),它不会进入那个挂载点所在的文件系统去复制文件。这意味着,只有源目录内相同文件系统下的文件和目录会被复制,位于其他文件系统中的内容则会被忽略。

这个选项通常在执行系统备份时非常有用,因为它可以防止 cp 命令进入外部驱动器或其他不相关的文件系统,从而确保备份仅包含指定文件系统内的内容。

示例

假设你要备份整个系统到另一个硬盘上,但你不希望包括外挂的硬盘或特定的系统挂载点(比如 /mnt 或 /media),这时使用 -x 或 --one-file-system 就非常有用了。

cp -a -x / /backup/

(将/备份到/backup,其中/backup是另一个外挂硬盘的挂载点)

-a 表示归档模式,尽可能地保留文件属性和权限,同时递归复制。-x 确保 cp 在遍历 / 的文件和目录时,一旦碰到其他文件系统的挂载点,比如 /mnt、/media 或者任何通过 fstab 自动挂载的分区,就不会进入那些挂载点去复制数据。

如果系统根目录 / 下存在指向其他文件系统的挂载点(例如,外部存储设备、其他分区等),使用 -x 选项将阻止 cp 命令复制那些位于不同文件系统中的数据。这样做可以避免不必要的数据复制,尤其是在执行系统级别的备份时,只想备份当前文件系统内的内容。

疑问

什么是“长选项的强制参数对短选项也是强制的”?

这句话的意思是:在命令行参数中,如果一个长选项(即用完整单词表示的选项,比如--file)后面需要跟一个强制参数(即这个选项必须有一个值),那么对应的短选项(一般是长选项的缩写,比如 -f)也需要跟这个强制参数。

例如,假设你有一个程序,其中有一个长选项 --file,它需要一个强制参数(比如一个文件名)。同时,这个长选项有一个对应的短选项 -f。在这种情况下,无论你是使用长选项还是短选项,都需要提供那个强制参数。

举例来说:

使用长选项:program --file filename.txt使用短选项:program -f filename.txt

在这两种情况下,你都需要提供一个文件名作为 --file 或 -f 的参数。

什么是“稀疏文件”?

稀疏文件是一种在文件系统中有效地存储数据的方式,它被设计用来保存大量连续零值的空间。在一个稀疏文件中,连续的零区域会被文件系统跳过并只记录位置信息,而不是实际保存这些零,从而节省磁盘空间。

当你读取稀疏文件时,文件系统会动态地将这些跳过的零值填充回去,让你感觉就像它们一直都在那里一样。

例如,假设你有一个 1GB 的文件,但其中 900MB 都是零。如果你将此文件保存为稀疏文件,那么它可能只占用 100MB 的磁盘空间(具体数字依赖于文件系统和操作系统),但当你打开和读取这个文件时,它仍然表现得像一个 1GB 的文件。

参考文章:稀疏文件(稀疏文档:sparse file)介绍(虚拟化技术、虚拟硬盘)(全零块)

相关文章

检察机关依法惩治电信网络诈骗及其关联犯罪
谁知道365足球网站

检察机关依法惩治电信网络诈骗及其关联犯罪

🕒 10-04 👁️ 1295
哪些乐高海淘网站可以直邮中国?12家可直邮中国的乐高海淘网站汇总!,海淘攻略
韩语自学一般学多久
365bet亚洲娱乐场

韩语自学一般学多久

🕒 07-25 👁️ 2410