linux复制cp命令_Linux Cp或复制命令

linux复制cp命令

linux复制cp命令

Linux popular command cp is used to copy files and folders. In this tutorial we will look different usage types of cp command.

Linux常用命令cp用于复制文件和文件夹。 在本教程中,我们将介绍cp命令的不同用法类型。

句法 (Syntax)

Syntax of cp command is like below.

cp命令的语法如下。

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

复制 (Copy)

We will just copy our source file to the destination file.

我们将只将源文件复制到目标文件。

$ ls                                                                                                                     
cat  test                                                                                                                             
$ cp cat cat2                                                                                                            
$ ls                                                                                                                     
cat  cat2  test

We have copied our source file cat into new file cat2

我们已经将源文件cat复制到了新文件cat2中

$ cp cat cat2 test/                                                                                                      
$ ls test/                                                                                                               
cat  cat2  ismail

We have copied cat and cat2 files into the test directory

我们已将catcat2文件复制到测试目录中

复制文件并重新命名(Copy File and Give New Name)

While copying the source file name is used as destination filename. If we need to rename newly created/copied file. We have to explicitly specify new name like below.

复制时,源文件名用作目标文件名。 如果需要重命名新创建/复制的文件。 我们必须明确指定新名称,如下所示。

In this example we will copy file named test and new file name will be test5

在此示例中,我们将复制名为test文件,新文件名为test5

$ cp  test test5

详细模式查看详细信息 (Verbose Mode See Details)

While copying bulk and a lot of files we may want to see details or save the details into a log file. It can be achieved with -v option like below

复制大量文件时,我们可能希望查看详细信息或将详细信息保存到日志文件中。 可以通过如下的-v选项来实现

$ cp -v cat cat2 bulk 
‘cat’ -> ‘bulk/cat’ 
‘cat2’ -> ‘bulk/cat2’

保留文件属性 (Preserve File Attributes)

While copying if we do not want to change modification data, time, access list and associated information about file we can use -p flag

在复制时,如果我们不想更改修改数据,时间,访问列表以及有关文件的相关信息,可以使用-p标志

$ ls -l 
total 0 
drwxr-xr-x 2 root root 27 Kas  4 01:59 bulk 
-rw-r--r-- 1 root root  0 Kas  4 01:55 cat 
-rw-r--r-- 1 root root  0 Kas  4 01:55 cat2 
drwxr-xr-x 3 root root 40 Kas  4 01:45 test 
$ cp -p cat2 cat3 
$ ls -l 
total 0 
drwxr-xr-x 2 root root 27 Kas  4 01:59 bulk 
-rw-r--r-- 1 root root  0 Kas  4 01:55 cat 
-rw-r--r-- 1 root root  0 Kas  4 01:55 cat2 
-rw-r--r-- 1 root root  0 Kas  4 01:55 cat3 
drwxr-xr-x 3 root root 40 Kas  4 01:45 test

复制多个文件 (Copying Multiple Files)

Sometimes we need to copy all files in a given path to the other path. In these situations we can use glob * which is provided by Linux bash. This will copy all contents to the given new path. In this example we will copy all files in this current working directory to the new path named new/ . We can also specify full path like /home/poftut .

有时我们需要将给定路径中的所有文件复制到另一个路径。 在这种情况下,我们可以使用Linux bash提供的glob * 。 这会将所有内容复制到给定的新路径。 在此示例中,我们将当前工作目录中的所有文件复制到名为new/的新路径。 我们还可以指定完整路径,例如/home/poftut

$ cp * new/

递归复制 (Recursive Copy)

Normally cp command copies only files in the current directory. If we have files and folders to copy cp command do not copies sub files and directories. We can enable recursive copy with -r option. This will copy all sub files and folders in given path.

通常, cp命令仅复制当前目录中的文件。 如果我们有文件和文件夹要复制cp命令,请不要复制子文件和目录。 我们可以使用-r选项启用递归复制。 这将复制给定路径中的所有子文件和文件夹。

$ cp -r -v bulk builk3 
‘bulk’ -> ‘builk3’ 
‘bulk/cat’ -> ‘builk3/cat’ 
‘bulk/cat2’ -> ‘builk3/cat2’

避免覆盖 (Avoid Overwriting)

By default cp command will overwrite same files on the destination path. If we are working in a critical folder we can avoid overwriting with -i . It will ask question is destination has same file or folder.

默认情况下, cp命令将覆盖目标路径上的相同文件。 如果我们在关键文件夹中工作,则可以避免使用-i覆盖。 它将询问问题是目的地具有相同的文件或文件夹。

$ cp -i cat bulk/ 
cp: overwrite ‘bulk/cat’? y

复制符号链接 (Copy Symbolic Links)

Symbolic links are used to  bind differents paths. Normally cp command do not process the symbolic link contents and do not copy. We can enable copying symbolic link contents with -s option.

符号链接用于绑定差异路径。 通常, cp命令不会处理符号链接的内容,也不会复制。 我们可以使用-s选项启用复制符号链接内容的功能。

$ cp -s /mnt/d  /opt/bak

创建硬链接 (Create Hard Links)

cp command can be used create hardlinks. Hard Link is link where source and destination files shares same data. Any change one of them will affect all source and destination file. We will provide -l which is used create hardlinks instead of copying them.

cp命令可用于创建硬链接。 硬链接是源文件和目标文件共享相同数据的链接。 其中任何一项更改都会影响所有源文件和目标文件。 我们将提供-l ,用于创建硬链接而不是复制它们。

$ cp -l /mnt/d  /opt/bak

创建软链接 (Create Soft Links)

We can also use cp to create Soft Links. Soft links provides links to the source file and only source file change will affect all soft links.

我们还可以使用cp创建软链接。 软链接提供了到源文件的链接,只有更改源文件才会影响所有软链接。

$ cp -s /mnt/d  /opt/bak

仅复制较新和不存在的文件 (Copy Only Newer and Unexisting Files)

This feature can be used to copy only files those are newer in the source and unexisting files. This is very useful for backup or sync purposes. We will use -u option for this feature.

此功能可用于仅复制源文件中较新的文件和不存在的文件。 这对于备份或同步非常有用。 我们将-u选项用于此功能。

$ cp -u /mnt/d  /opt/bak

防止覆盖或无障碍 (Prevent Overwrite or No Clobber)

We can strictly prevent overwriting files in the destination. We will use n option in order to no clobber.

我们可以严格防止覆盖目标文件。 我们将使用n选项以消除混乱。

$ cp -n /mnt/d  /opt/bak

保留所有文件属性 (Preserve All File Attributes)

Files and folders have some file system and operating system related attributes. While copying some of them are changed according to the destination path. We can preserve these attributes with --preserve=all option like below.

文件和文件夹具有一些文件系统和操作系统相关的属性。 复制时,其中一些会根据目标路径进行更改。 我们可以使用--preserve=all选项保留这些属性,如下所示。

$ cp --preserve=all /mnt/d  /opt/bak
LEARN MORE  How To Recover Data with ddrescue Command?
了解更多信息如何使用ddrescue命令恢复数据?

Linux Cp或复制命令信息 (Linux Cp or Copy Command Infografic)

Linux Cp or Copy Command Infografic
Linux Cp or Copy Command Infografic
Linux Cp或复制命令信息

翻译自: https://www.poftut.com/linux-cp-or-copy-command/

linux复制cp命令

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值