命令行大全

cp

瑞兹 发表于 2021-01-05 09:12浏览次数:

在类似Unix的操作系统上,cp命令可复制文件和目录。

查看英文版

目录:

1 cp 运行系统环境

2 cp 描述

3 cp 语法

4 cp 例子

cp 运行系统环境

Unix&Linux

cp 描述

该命令的一般形式是cp source destination,例如:

cp myfile.txt myfilecopy.txt

与许多核心Linux命令一样,如果cp命令成功执行,则默认情况下不会显示任何输出。要在复制文件时查看输出,请使用-v(详细)选项。

默认情况下,cp会不询问而覆盖文件。如果目标文件名已经存在,则其数据将被销毁。如果要在覆盖文件之前提示您进行确认,请使用-i(交互式)选项。

The general form of the command is cp source destination, for example:

cp myfile.txt myfilecopy.txt

Like many core Linux commands, if the cp command is successful, by default, no output is displayed. To view output when files are copied, use the -v (verbose) option.

By default, cp will overwrite files without asking. If the destination file name already exists, its data will be destroyed. If you want to be prompted for confirmation before files are overwritten, use the -i (interactive) option.

查看英文版

查看中文版

cp 语法

cp [option]... [-T] source destination
cp [option]... source... directory
cp [option]... -t directory source...
cp --help
cp --version

选件

-a,-
-archive
-dR --preserve = ALL相同。执行复制时,请尝试保留尽可能多的原始文件结构,属性和关联的元数据。如果您正在运行SELinux,则此元数据包括安全上下文数据。
-attributes-only 不复制文件数据,仅创建具有相同属性的文件。如果目标文件已经存在,请不要更改其内容。您可以使用--preserve选项精确控制要复制的属性。
--backup [ =control] 对每个现有目标文件进行备份,否则将被覆盖或删除。该控制参数指定了版本控制的方法来使用; 有关详细信息,请参见版本控制。

作为一种特殊情况,当sourcedest是相同的常规文件时,cp --force --backup会备份
-b 类似于--backup,但不接受控制参数;始终使用默认控制方法。
--copy-contents 递归操作时,复制特殊文件的内容,例如FIFO和/ dev中的设备。您通常不希望使用此选项,因为它可能会产生不希望的结果,例如永久挂起或填满整个磁盘。但是,此选项可用于特殊的专业用例。
-d 复制符号链接本身(而不是它们引用的文件),并保留副本中源文件之间的硬链接。与“ --no-dereference --preserve = links ”相同。
-f
--force
如果无法打开现有的目标文件,请将其删除,然后重试。如果使用-n / --no-clobber选项,则此选项无效。但是,它独立于-i / --interactive来应用;这两个选项都不能消除另一个的影响。
-i,--
interactive
覆盖前提示(覆盖先前的-n选项)。
-H 遵循命令行上指定的符号链接,但保留发现的链接。

如果命令行上的参数之一是符号链接,请复制引用的文件,而不是链接本身。但是,如果在递归遍历过程中发现符号链接,则会将其作为符号链接而不是常规文件进行复制。
-l,--
link
创建指向文件的硬链接,而不是复制它们。
-L,--
dereference
始终遵循源代码中的符号链接; 如果source是符号链接,请复制链接到的文件,而不是符号链接本身。指定此选项后,cp无法在目标副本中创建符号链接。
-n,--
no-clobber
不要覆盖现有文件。如果先前指定了-i / --interactive,则此选项将覆盖它。不能使用-b / --backup来指定此选项,因为仅在文件被覆盖时才创建备份。
-P
--no-dereference
切勿遵循源代码中的符号链接; 将符号链接复制为符号链接。但是,仍然可以遵循目标中遇到的现有符号链接。
-p --preserve = mode,ownership,timestamps相同
--preserve = [ attr_list ] 保留指定的属性,以逗号分隔。属性是:
mode 保留文件模式位(由chmod设置)和所有ACL。
ownership 保留所有者和组(按chown设置)。保留这些属性的能力与使用chown相同。
timestamps 如果可能的话,保留最后一次文件访问和修改的时间(atimemtime,由touch设置)。
links 在目标文件中保留源文件之间的所有链接。使用-L-H,此选项可能会将符号链接复制为硬链接。
context 保留源文件的SELinux安全上下文,否则将由于详细的诊断而失败。
Xattr 保留源文件的扩展属性,否则将因详细诊断而失败。
all 保留以上所有内容。与单独指定以上所有属性相同,不同之处在于无法复制contextxattr不会给出失败的退出状态。
如果未指定,则attr_list的默认值为“ mode,ownership,timestamps ”。
--no-preserve = attr_list 不要保留指定的属性。
--parents 如果需要,在复制到目标目录时,根据source中指定的路径名,在目标中创建丢失的父目录。

例如,“ cp --parents dir2 / dir3 / file dir1 ”创建目标文件“ dir1 / dir2 / dir3 / file ”,即使dir1下目前不存在dir2dir3
-R
-r,--recursive
递归复制目录。
--reflink = [when] 如果目标文件系统支持,请执行优化的CoW(“写时复制”)克隆。生成的副本将在磁盘上与原始文件共享相同的字节,直到修改副本为止。请注意,这意味着如果源字节已损坏,则目标将共享损坏的数据。

参数定义写入时复制不是由文件系统的目的地支持会发生什么。如果always(默认值),复制失败。如果为auto,将使用标准复制行为进行复制。
--remove-destination 尝试打开每个目标文件之前,先删除它(与--force选项相反,该选项仅在尝试打开失败后才删除目标文件)。
--sparse =when 控制稀疏文件的创建。

稀疏文件包含“空洞”,其中的空洞是零字节序列,不占用物理磁盘空间。读取文件时,孔将读取为零。由于许多文件包含长的零序列,因此可以节省磁盘空间。缺省情况下,cp检测稀疏文件并创建稀疏目标文件。

参数定义了CP当检测到源文件是稀疏应该做的:

auto 如果源稀疏,请尝试使目标稀疏。如果目标存在并且是非常规文件,请不要尝试使其稀疏。这是默认值。
always 对于源中每个零字节的足够长的序列,即使输入文件不是稀疏的,也要尝试在目标中打稀疏。如果源文件系统不支持稀疏文件,这将非常有用。可以在目标文件系统上适当地创建稀疏文件。
never 切勿使输出文件稀疏。某些特殊文件(例如交换文件)绝对不能稀疏。在这些情况下,应使用此选项。
--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
仅在执行命令的文件系统上操作。如果cp试图越过边界到另一个文件系统,则这些文件将被跳过。这包括网络驱动器,另一个分区-驻留在具有不同mount点的文件系统上的任何文件。

代表安装点本身的目录被复制但未被遍历。

如果指定了-v,您将确切看到跳过了哪些文件。
--help 显示帮助消息,然后退出。
--version 输出版本信息,然后退出。

默认情况下,稀疏的启发式算法会检测到稀疏的文件,并且相应的目标文件也会变得稀疏。这是--sparse = auto选择的行为。指定--sparse = always总是在文件包含足够长的零字节序列时创建稀疏目标文件。使用--sparse = never禁止创建稀疏文件。

--reflink [ =always]被指定时,CP执行轻型副本,其中所述数据块改性时仅复制。如果不可能,则复制失败;否则,复制将失败。或者,如果指定了--reflink = auto,则cp将退回到标准复制操作。

版本控制

使用-b / --backup时,备份后缀为'  ',除非使用--suffixSIMPLE_BACKUP_SUFFIX进行设置。可以通过--backup =控制选项或通过VERSION_CONTROL 环境变量来选择版本控制方法。控制VERSION_CONTROL的可能值为:

noneoff 切勿进行备份(即使给出了--backup)。
numberedt 进行编号备份。
existingnil Numbered是否存在编号备份,simple说明。
simplenever 始终进行简单的备份。

作为一个特例,CP使备份时,forcebackup选项给出的源目的是相同的名称为现有,普通文件。

cp [option]... [-T] source destination
cp [option]... source... directory
cp [option]... -t directory source...
cp --help
cp --version

Options

-a,
--archive
Same as -dR --preserve=ALL. When performing the copy, attempt to preserve as much of the original file structure, attributes, and associated metadata as possible. This metadata includes security context data if you are running SELinux.
--attributes-only Don't copy the file data, only create a file with the same attributes. If the destination file already exists, don't alter its contents. You can control exactly which attributes are copied with the --preserve option.
--backup[=control] Make a backup of each existing destination file that would otherwise be overwritten or removed. The control parameter specifies what version control method to use; see version control for details.

As a special case, cp --force --backup makes a backup of source when source and dest are the same, regular file.
-b Like --backup, but does not accept a control argument; the default control method is always used.
--copy-contents When operating recursively, copy contents of special files, such as FIFOs and devices found in /dev. You usually don't want to use this option, because it can have undesired results, such as hanging forever or filling up your entire disk. However, this option is available for special, expert use cases.
-d Copy symbolic links themselves, rather than the files they refer to, and preserve hard links between source files in the copies. Same as "--no-dereference --preserve=links".
-f,
--force
If an existing destination file cannot be opened, remove it and try again. This option has no effect if the -n/--no-clobber option is used. However, it applies independently of -i/--interactive; neither option cancels the effect of the other.
-i,
--interactive
Prompt before overwrite (overrides a previous -n option).
-H Follow symlinks specified on the command line, but preserve discovered links.

If one of the arguments on the command line is a symbolic link, copy the referenced file, not the link itself. However, if a symbolic link is discovered during recursive traversal, it's copied as a symlink, not a regular file.
-l,
--link
Create hard links to files instead of copying them.
-L,
--dereference
Always follow symbolic links in source; if source is a symlink, copy the file linked to rather than the symlink itself. When this option is specified, cp cannot create symlinks in the destination copies.
-n,
--no-clobber
Do not overwrite an existing file. If -i/--interactive was previously specified, this option overrides it. This option cannot be specified with -b/--backup, because backups are only created when a file would be overwritten.
-P,
--no-dereference
Never follow symbolic links in source; copy symlinks as symlinks. Existing symlinks encountered in the destination may still be followed, however.
-p Same as --preserve=mode,ownership,timestamps.
--preserve=[attr_list] Preserve the specified attributes, separated by a comma. Attributes are:
mode Preserve file mode bits (as set with chmod), and any ACLs.
ownership Preserve owner and group (as set with chown). Ability to preserve these attributes is restricted in the same way as using chown.
timestamps Preserve time of last file access and modification (atime and mtime, as set with touch), if possible.
links Preserve in the destination files any links between the source files. With -L or -H, this option can potentially copy symbolic links as hard links.
context Preserve SELinux security context of source files, or fail with verbose diagnostics.
xattr Preserve extended attributes of source files, or fail with verbose diagnostics.
all Preserve all of the above. Same as specifying all the above attributes individually, with the exception that failing to copy context or xattr will not give an exit status of failure.
If not specified, the default value of attr_list is "mode,ownership,timestamps".
--no-preserve=attr_list Don't preserve the specified attributes.
--parents Create missing parent directories in the destination, if necessary, when copying to target directory, according to the pathname specified in source.

For instance, "cp --parents dir2/dir3/file dir1" creates the destination file "dir1/dir2/dir3/file", even if dir2 and dir3 do not presently exist under dir1.
-R,
-r,
--recursive
Copy directories recursively.
--reflink=[when] Perform an optimized CoW ("copy-on-write") clone, if the destination filesystem supports it. The resulting copy will share the same bytes on disk as the original file, until the copy is modified. Be aware that this means if the source bytes are corrupted, the destination will share the corrupted data.

The when parameter defines what will happen if copy-on-write is not supported by the destination filesystem. If when is always (the default), the copy fails. If auto, copying will proceed, using standard copy behavior.
--remove-destination Remove each existing destination file before attempting to open it (contrast with the --force option, which only removes the destination after a failed attempt to open).
--sparse=when Control creation of sparse files.

A sparse file contains "holes", where a hole is a sequence of zero bytes that occupies no physical disk space. When the file is read, the holes are read as zeros. This can conserve disk space since many files contain long sequences of zeros. By default, cp detects sparse files and creates sparse destination files as well.

The when parameter defines what cp should do when a source file is detected to be sparse:

auto If the source is sparse, attempt to make the destination sparse. If destination exists and is a non-regular file, do not attempt to make it sparse. This is the default.
always For each sufficiently long sequence of zero bytes in the source, attempt to make a sparse hole in the destination, even if the input file is not sparse. This can be useful if the source filesystem does not support sparse files; a sparse file can be appropriately created on the destination filesystem.
never Never make the output file sparse. Some special files, such as a swap file, must never be sparse. In these cases, this option should be used.
--strip-trailing-slashes Remove any trailing slashes from each source argument.
-s,
--symbolic-link
Make symbolic links instead of copying the files themselves. All source files must be absolute pathnames starting with a slash, unless the destination files are in the current directory.
-S,
--suffix=suffix
Override the usual backup suffix.
-t,
--target-directory=directory
Copy all source arguments into directory
-T,
--no-target-directory
Treat destination 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
Verbose mode; explain what is being done.
-x,
--one-file-system
Only operate on the filesystem where the command was executed. If cp tries to cross the boundary to another filesystem, those files will be skipped. This includes networked drives, another partition — any file that resides on a filesystem with a different mount point.

The directory representing the mount point itself is copied, but not traversed.

If -v is specified, you will see exactly which files are skipped.
--help Display a help message, and exit.
--version Output version information, and exit.

By default, sparse source files are detected by a crude heuristic and the corresponding destination file is made sparse as well. That is the behavior selected by --sparse=auto. Specify --sparse=always to create a sparse destination 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, cp performs 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, cp falls back to a standard copy operation.

Version control

When using -b/--backup, the backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX. The version control method may be selected via the --backup=control option or through the VERSION_CONTROL environment variable. The possible values of control or VERSION_CONTROL are:

noneoff Never make backups (even if --backup is given).
numberedt Make numbered backups.
existingnil Numbered if numbered backups exist, simple otherwise.
simplenever 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 destination are the same name for an existing, regular file.

查看英文版

查看中文版

cp 例子

假设您的工作目录中有一个名为picture.jpg的文件,并且您想要复制一个名为picture-02.jpg的文件。您将运行以下命令:

cp picture.jpg picture-02.jpg

...然后文件被复制。在这里,picture.jpg是复制操作的源,而picture-02.jpg是目标。这两个文件现在都存在于您的工作目录中。

源文件和目标文件也可以位于不同的目录中。例如,

cp /home/chuck/pictures/picture.jpg /home/chuck/backup/picture.jpg

...在目录/ home / chuck / backup中复制文件/home/chuck/pictures/picture.jpg。目标文件也命名为picture.jpg

如果您是用户chuck,则可以使用代字号(“  ”)缩写主目录(“ / home / chuck ”)。例如,

cp〜/ pictures / picture.jpg〜/ backup / picture.jpg

...由chuck运行时,其功能与上述命令相同。

将多个文件复制到目录

或者,也许您要将多个文件复制到另一个目录。为此,您可以指定多个文件作为源,并指定目录名作为目标。假设您是sally的用户,并且在/ home / sally / pictures /目录中有一堆文件,名为picture-01.jpgpicture-02.jpg等,您想将它们复制到目录home/ sally /picture/backup/中。这个命令可以解决问题:

cp〜/ pictures / picture-*。jpg〜/ picture-backup

在这里,我们使用通配符(星号“ * ”)表示源文件是目录/ home / sally / pictures中所有名称以“ picture- ”开头且扩展名为“ .jpg ”的文件。假设该目录已存在,它们将被复制到目录/ home / sally / picture-backup中。如果不存在,cp会给您一条错误消息,并且不会复制任何文件。

您还可以一个接一个地指定多个源文件,并且cp期望final参数是目录名,然后将它们全部复制到该目录中。例如,

cp〜/ pictures / picture-01.jpg〜/ pictures / picture-02.jpg〜/ picture-backup

...只会将/home/sally/picture-01.jpg/home/sally/picture-02.jpg这两个文件复制到目录/ home / sally / picture-backup中

递归复制文件

您可以使用-r选项使用cp将整个目录结构从一个位置复制到另一个位置,以执行递归复制。假设您是steve用户,并且有一个目录/ home / steve / files,其中包含许多文件和子目录。您想要将所有这些文件以及所有子目录(及其包含的文件和子目录)复制到新位置/ home / steve / files-backup。您可以使用以下命令复制所有文件:

cp -R〜/文件〜/文件备份

...并将整个目录结构复制到目录/ home / steve / files-backup。执行递归复制时:

  • 如果目录文件备份已经存在,则目录文件将放置在其中。
  • 如果文件备份不会存在,它将会被创建和内容的的文件目录将放在里面。

创建符号链接,而不是复制数据

另一个有用的技巧是使用cp创建到源文件的符号链接。您可能已经熟悉使用ln命令创建符号链接。cp是一次创建多个符号链接的好方法。

如果指定-s选项,则cp将创建符号链接。因此,例如

cp -s file.txt file2.txt

...创建一个符号链接file2.txt,它指向file.txt

您还可以从多个源文件创建符号链接,将目录指定为目标。

注意

要在另一个目录中创建符号链接,cp需要您在源文件名中指定完整路径名,包括完整目录名。相对路径将不起作用。

假设您是melissa用户,并且在/ home / melissa / myfiles目录中有一组文件file01.txtfile02.txt等。您想要在现有目录/ home / melissa / myfiles2中创建指向这些文件的符号链接。这个命令可以解决问题:

cp -s〜/ myfiles / file * .txt〜/ myfiles2

现在,目录myfiles2包含指向目录/ home / melissa / myfiles中文件* .txt的符号链接。该myfiles2目录必须已经存在,操作成功; 如果不存在,cp会给您一条错误消息,并且不会复制任何内容。

这也将与递归副本一起使用。所以命令:

cp -R -s〜/ myfiles〜/ myfiles2

...将重新创建/ home / melissa / myfiles的目录结构,包括所有子目录及其内容;任何文件都将创建为原始文件的符号链接,但目录不是符号链接,而只是常规目录。如果myfiles2已经存在,则cp在其中创建一个名为myfiles的目录,其中包含目录结构和符号链接;如果myfiles2还不存在,它将被创建,并包含指向myfiles包含的文件的子目录和符号链接。

您可以为cp提供其他选项,以影响其行为。以下各节中列出了这些命令以及精确的命令语法。

将文件复制到同一目录

cp origfile新文件

在名为origfile的工作目录中创建文件的副本。该副本将被命名为newfile,并将位于工作目录中。

Let's say you have a file named picture.jpg in your working directory, and you want to make a copy of it called picture-02.jpg. You would run the command:

cp picture.jpg picture-02.jpg

...and the file is copied. Here, picture.jpg is the source of the copy operation, and picture-02.jpg is the destination. Both files now exist in your working directory.

The source and destination files may also reside in different directories. For instance,

cp /home/chuck/pictures/picture.jpg /home/chuck/backup/picture.jpg

...makes a copy of the file /home/chuck/pictures/picture.jpg in the directory /home/chuck/backup. The destination file is also named picture.jpg.

If you are the user chuck, you can abbreviate your home directory ("/home/chuck") using a tilde ("~"). For instance,

cp ~/pictures/picture.jpg ~/backup/picture.jpg

...functions the same as the above command when it is run by chuck.

Copying multiple files to a directory

Or, perhaps you want to copy multiple files into another directory. To accomplish this, you can specify multiple files as the source, and a directory name as the destination. Let's say you are the user sally, and you have a bunch of files in the directory /home/sally/pictures/ named picture-01.jpgpicture-02.jpg, etc. and you want to copy them into the directory /home/sally/picture-backup/. This command will do the trick:

cp ~/pictures/picture-*.jpg ~/picture-backup

Here, we use a wildcard (the asterisk, "*") to indicate that the source files are all the files in the directory /home/sally/pictures whose name starts with "picture-" and has the extension ".jpg". They are copied into the directory /home/sally/picture-backup, assuming that directory already exists. If it doesn't exist, cp gives you an error message, and no files are copied.

You can also specify multiple source files one after the other, and cp will expect that the final argument is a directory name, and copy them all there. For instance,

cp ~/pictures/picture-01.jpg ~/pictures/picture-02.jpg ~/picture-backup

...will copy only those two files, /home/sally/picture-01.jpg and /home/sally/picture-02.jpg, into the directory /home/sally/picture-backup.

Copying files recursively

You can use cp to copy entire directory structures from one place to another using the -R option to perform a recursive copy. Let's say you are the user steve and you have a directory, /home/steve/files, which contains many files and subdirectories. You want to copy all those files, and all the subdirectories (and the files and subdirectories they contain), to a new location, /home/steve/files-backup. You can copy all of them using the command:

cp -R ~/files ~/files-backup

...and the entire directory structure is copied to the directory /home/steve/files-backup. When performing a recursive copy:

  • If the directory files-backup already exists, the directory files will be placed inside.
  • If files-backup does not already exist, it will be created and the contents of the files directory will be placed inside it.

Creating symbolic links instead of copying data

Another useful trick is to use cp to create symbolic links to your source files. You may already be familiar with using the ln command to create symlinks; cp is a great way to create multiple symlinks all at once.

cp creates symbolic links if you specify the -s option. So, for instance,

cp -s file.txt file2.txt

...creates a symbolic link, file2.txt, which points to file.txt.

You can also create symbolic links from multiple source files, specifying a directory as the destination.

Note

To create symbolic links in another directory, cp needs you to specify the full pathname, including the full directory name, in your source file name(s). Relative paths will not work.

Let's say you are user melissa and you have a set of files, file01.txtfile02.txt, etc. in the directory /home/melissa/myfiles. You want to create symbolic links to these files in the existing directory /home/melissa/myfiles2. This command will do the trick:

cp -s ~/myfiles/file*.txt ~/myfiles2

The directory myfiles2 now contain symbolic links to the file*.txt in the directory /home/melissa/myfiles. The myfiles2 directory must already exist for the operation to succeed; if it doesn't exist, cp gives you an error message and nothing is copied.

This will work with a recursive copy, as well. So the command:

cp -R -s ~/myfiles ~/myfiles2

...will re-create the directory structure of /home/melissa/myfiles, including any subdirectories and their contents; any files will be created as symlinks to the originals, but the directories are not symbolic links, only regular directories. If myfiles2 already exists, cp creates a directory inside it called myfiles which contains the directory structure and symlinks; if myfiles2 does not already exist, it will be created, and contain the subdirectories and symlinks to the files that myfiles contains.

There are other options you can provide to cp that affect its behavior. These are listed, along with the precise command syntax, in the following sections.

Make a copy of a file into the same directory

cp origfile newfile

Creates a copy of the file in the working directory named origfile. The copy will be named newfile, and will be located in the working directory.

Caution

If the destination file newfile already exists, it will be overwritten without a confirmation prompt. This is the default behavior for all cp operations.

If you want to be prompted before overwriting a file, use the -i (interactive) option. For example:

cp -i oldfile newfile

If newfile already exists, you will be prompted:

cp: overwrite ‘newfile’?

If you type y (or yesYYES, or any other combination of upper and lowercase of these), then newfile will be overwritten with a copy of origfile. Typing anything else will abort the operation.

Copy a file into another directory

cp origfile /directory/subdirectory

Creates a copy of the file in the working directory named origfile. The copy will be located in the directory /directory/subdirectory, and will be named origfile.

cp origfile /directory/subdirectory/.

Same as the above command. The slash-dot (/.) is implied in the above form of the command. (The dot is a special file in every Linux directory which means "this directory.")

Copy a file into another directory, and give it a new name

cp origfile /directory/subdirectory/newfile

Creates a copy of the file in the working directory named origfile. The copy will be named newfile, and will be located in the directory /directory/subdirectory.

Copy multiple files into another directory, using a wildcard

cp file* /directory/subdirectory

Copy every file in the working directory whose name begins with file into the directory /directory/subdirectory. The asterisk ("*") is a wildcard — a special character which expands to match other characters. Specifically, the asterisk wildcard matches zero or more non-whitespace characters. For instance, this command will copy any files named filefile001file.txtfileone.jpgfile-archive.zip, etc.

cp file*.jpg /directory/subdirectory

Copy every file in the working directory whose name begins with file, and ends with the file extension .jpg. For instance, it would make copies of any files named filefile001.jpgfile002.jpg, or file-new.jpg, etc. The copies will be placed into the directory /directory/subdirectory.

Copy an entire directory structure to another location

cp -R /one/two /three/four

Copy the directory two (located in the directory /one), and everything two contains, into the destination directory /three/four. The result will be called /three/four/two. The directory /three must already exist for the command to succeed. If the directory four does not already exist in the directory /three, it will be created.

cp file1.txt newdir

Copies the file1.txt in the working directory to the newdir subdirectory.

cp /home/public_html/mylog.txt /home/public_html/backup/mylog.bak

Copies the file mylog.txt in the public_html directory into the public_html/backup directory as mylog.bak.

cp -u *.txt newdir

Copy all files ending in .txt into the newdir directory, but only if the files do not already exist in the new directory, or if the files being copied are newer.

cp -R /home/hope/files/* /home/hope/backup

Recursively copies all the files, directories, and subdirectories in the /home/hope/files directory into the /home/hope/backup directory. If the directory backup does not exist in the directory /home/hope, it will be created.

cp --backup origfile newfile

If newfile already exists, make a backup of the existing newfile before overwriting it with a copy of origfile. By default, the backup of newfile will be named newfile~.

cp --backup=numbered origfile newfile

If newfile already exists, make a backup of the existing newfile before overwriting it with a copy of origfile. The backup of newfile will be named newfile.~1~ if no other backup exists, or newfile.~2~ if newfile.~1~ exists, etc.

查看英文版

查看中文版

发表评论
评论列表
共0条
点击加载更多