unix和linux命令_Linux / UNIX中的cp命令

unix和linux命令

In this guide, we focus on cp command in Linux/Unix systems. cp command – short for copy – is a command used for copying files and directories in Linux. It’s a command that is widely used both by Linux beginners and seasoned experts on a regular basis. Let’s dive in and see how this command is used.

在本指南中,我们重点介绍Linux / Unix系统中的cp命令。 cp命令–复制的缩写–是用于在Linux中复制文件和目录的命令。 这是Linux初学者和经验丰富的专家定期广泛使用的命令。 让我们深入看看如何使用此命令。

Linux cp命令示例 (Linux cp command example)

The basic command structure of cp command is as follows:

cp命令的基本命令结构如下:

$ cp [options] source dest

Let’s look at some examples of using cp command to copy files and folders in Linux.

让我们看一些在Linux中使用cp命令复制文件和文件夹的示例。

将文件从当前目录复制到子目录 (Copy a file from current directory to subdirectory)

To copy a file from the present working directory to a subdirectory use the syntax below:

要将文件从当前工作目录复制到子目录,请使用以下语法:

$ cp file directory

Example

For example, to copy a file file1.txt to a sub directory called data execute the cp command as follows. Later use the ls command to verify the existence of the file in the directory.

例如,要将文件file1.txt复制到名为data的子目录,请执行cp命令,如下所示。 以后使用ls命令来验证目录中文件的存在。

$ cp file1.txt data

Sample output

样品输出

将文件从当前目录复制到绝对目标路径 (Copy a file from current directory to an absolute destination path)

To copy a file from the present working directory to a different destination directory or absolute path, use the syntax below.

要将文件从当前工作目录复制到其他目标目录或绝对路径,请使用以下语法。

$ cp file /path/to/directory

Example

$ cp file1.txt /home/james/

Output

输出量

将文件从绝对源路径复制到绝对路径 (Copy a file from absolute source path to an absolute path)

To copy a file(s) from a different file path to another absolute path use the syntax:

要将文件从其他文件路径复制到另一个绝对路径,请使用以下语法:

cp /path/to/source/file /path/to/destination/directory

Example

$ cp /home/james/file1.txt /opt/data

Sample output

样品输出

复制多个文件 (Copy multiple files)

Linux cp command also allows you to copy more than one file at a go using the syntax below.

Linux cp命令还允许您使用以下语法一次复制多个文件。

$ cp file1 file2 .... /path/to/directory

Example

$ cp file1.txt file2.txt file3.txt /home/james/

Sample output

样品输出

复制带有详细输出的文件 (Copy file with verbose output)

To display verbose output, use the -v flag as shown.

要显示详细的输出,请使用-v标志,如图所示。

cp -v file1.txt file2.txt  /home/james

Sample output

样品输出

交互式复制文件 (Copy files interactively)

To copy files interactively i.e to provoke a prompt from the Linux shell, use the -i flag as shown.

要以交互方式复制文件,即从Linux Shell引起提示,请使用-i标志,如图所示。

$ cp -i file1.txt /home/james

Sample output

样品输出

复制与某个文件扩展名关联的文件 (Copy files associated with a certain file extension)

If you wish to copy a number of files with the same file extension, say .txt or .pdf follow the example below.

如果您希望复制多个具有相同文件扩展名的文件,请按照以下示例说.txt.pdf

$ cp *.txt  /home/james

递归复制Linux目录 (Linux Copy Directory Recursively)

If you want to copy a directory alongside all its contents, use the -R attribute followed by the directory name as shown.

如果要复制目录及其所有内容,请使用-R属性,后跟目录名称,如图所示。

$ cp -R data /home/james

Sample output

样品输出

To copy the contents of a directory but not the directory itself, run the command as follows.

要复制目录的内容而不是目录本身,请按以下方式运行命令。

$ cp -R data/* /home/james

Sample output

样品输出

Linux man cp (Linux man cp)

We can run man cp command to look at the documentation of cp command. This is useful to find out the right options to use with the cp command.

我们可以运行man cp命令来查看man cp命令的文档。 这对于找出与cp命令一起使用的正确选项很有用。

man cp

Sample output

样品输出

摘要 (Summary)

Linux cp command helps us in copying files and folders easily. It’s one of the most used commands. The man page of cp command is very helpful in finding the suitable options to use.

Linux cp命令可帮助我们轻松复制文件和文件夹。 这是最常用的命令之一。 cp命令的手册页对找到合适的选项很有帮助。

翻译自: https://www.journaldev.com/26120/cp-command-in-linux-unix

unix和linux命令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值