linux cp目录
Linux provides cp
command in order to copy files, folders, and directories. But sometimes we may have some issues if we want to copy a folder or directory. In this tutorial, we will learn how to copy folders and directories with the cp command and provides solutions for problems.
Linux提供cp
命令以复制文件,文件夹和目录。 但是有时候,如果我们要复制文件夹或目录,可能会遇到一些问题。 在本教程中,我们将学习如何使用cp命令复制文件夹和目录,并提供解决问题的方法。
句法 (Syntax)
cp
command has following general syntax which can be used for all different kind of copy operations.
cp
命令具有以下通用语法,可用于所有不同种类的复制操作。
cp OPTIONS SOURCE DESTINATION
- OPTIONS are used to set different behavior to the `cp` command like recursive OPTIONS用于为`cp`命令设置不同的行为,例如递归
- `SOURCE` is used to specify the source directory. The source can be a relative or absolute path `SOURCE`用于指定源目录。 源可以是相对路径或绝对路径
- `DESTINATION` is used to specify the destination directory. The source can be a relative or absolute path DESTINATION用于指定目标目录。 源可以是相对路径或绝对路径
复制空目录(Copy Empty Directory)
We will start with a simple example of cp
command. We will simply copy an empty directory by providing -r
providing an option to the cp
command. If there is some content in the directory the cp
command will not work without any option.
我们将从一个简单的cp
命令示例开始。 我们将通过提供-r
为cp
命令提供一个选项来简单地复制一个空目录。 如果目录中有某些内容,则cp
命令如果没有任何选项将不起作用。
We can see that there is an error like
我们可以看到这里有一个错误
cp: -r not specified; omitting directory 'backup/'
So we will provide the -r
option for recursivity.
因此,我们将提供-r
选项以实现递归。
$ cp backup backup2
复制目录和所有内容(Copy Directory and All Contents)
We can copy all subfolders, directories and contents with by using -r
option like below. In this example, we will copy the folder named nmap
into /home/ismail/nmap
. The newly created directory named will be nmap
too.
我们可以使用-r
选项复制所有子文件夹,目录和内容,如下所示。 在此示例中,我们将名为nmap
的文件夹复制到/home/ismail/nmap
。 新创建的目录也将是nmap
。
$ cp -r nmap /home/ismail/nmap
在保留属性的同时复制目录和所有内容 (Copy Directory and All Contents While Preserving Attributes)
Linux cp
command also provides -a
option which can be used to preserve attributes of the copied files and folders. Attributes hold special information like readonly, selinux etc . -a
option also provides the recursivity, so we do not need to provide -r
option to copy sub files and folders.
Linux cp
命令还提供了-a
选项,该选项可用于保留复制的文件和文件夹的属性。 属性包含特殊信息,例如readonly,selinux等。 -a
选项还提供递归性,因此我们不需要提供-r
选项来复制子文件和文件夹。
$ cp -a nmap nmap-backup
详细模式 (Verbose Mode)
If we want to list operations about copy we can use -v
option. -v
option is named as verbose mode
. We can also call it debug mode. In verbose mode command will print all copy operations to the standard output which is generally our terminal.
如果要列出有关复制的操作,可以使用-v
选项。 -v
选项被称为verbose mode
。 我们也可以将其称为调试模式。 在详细模式下,命令会将所有复制操作打印到通常是我们终端的标准输出中。
$ cp -r -v nmap nmap-backup
使用Rsync命令复制到远程服务器(Copy To The Remote Server with Rsync Command)
rsync
is an alternative method to copy files and folders in to remote servers. As cp
is used to copy locally rsync
provides remote or network wide copy operations. We can use -avz
options in order to copy from local to remote or from remote to local like below. In this example, we will copy local files to the remote server with IP address 192.168.142.144 .
rsync
是将文件和文件夹复制到远程服务器的另一种方法。 由于cp
用于本地复制,因此rsync
提供了远程或网络范围的复制操作。 我们可以使用-avz
选项,以便从本地复制到远程或从远程复制到本地,如下所示。 在此示例中,我们将本地文件复制到IP地址为192.168.142.144的远程服务器。
$ rsync -avz nmap 192.168.142.144:/home/ismail/backup/
使用Scp命令复制到远程服务器(Copy To The Remote Server with Scp Command)
scp
command has similar functionality to the rsync
command. scp
copies files and folders over the network to the remote or local systems. scp
mainly uses SSH protocol to copy over network. We can use very similar options of the cp
command. In order to copy local to remote or remote to local in a recursive manner, we can use -r
option with the scp
command too. In this example, we will copy files from nmap
directory to the remote IP address 192.168.142.144
/home/ismail/backup
directory.
scp
命令具有与rsync
命令类似的功能。 scp
通过网络将文件和文件夹复制到远程或本地系统。 scp
主要使用SSH协议通过网络进行复制。 我们可以使用cp
命令的非常相似的选项。 为了以递归方式将本地复制到远程或从远程复制到本地,我们也可以在scp
命令中使用-r
选项。 在此示例中,我们将文件从nmap
目录复制到远程IP地址192.168.142.144
/home/ismail/backup
目录。
$ scp -r nmap 192.168.142.144:/home/ismail/backup/
We can see that during secure copy operation the files size, complete percentage, estimated remaining time information also provided.
我们可以看到,在安全复制操作期间,还提供了文件大小,完整百分比,估计的剩余时间信息。
linux cp目录