已翻译 by Luox

This commit is contained in:
Luoxcat 2014-01-27 14:31:35 +08:00
parent 9dba11d766
commit ac5ffc2308
2 changed files with 245 additions and 188 deletions

View File

@ -1,188 +0,0 @@
15 Linux cp Command Examples - Create a Copy of Files and Directories
================================================================================
Copying files or directories is one of basic activity in every operating system. Backup activity is basically is creating a copy of files and directories. On Linux system, we can use **cp** command to do it.
### What is copy command ###
As we mentioned above, **cp** command is a command to create copy of files and directories. Heres some samples of cp command that might useful in day-to-day operation
#### 1. Run cp without any options ####
This is a very basic cp usage. To copy a file name myfile.txt from one location to another location, we can type like this :
$ cp myfile.txt /home/pungki/office
![Copy without options](http://linoxide.com/wp-content/uploads/2014/01/cp_default.png)
If we dont type absolute path, it mean that we are copying a file on current directory. From example above, **myfile.txt** is located in **/home/pungki/Documents**. We dont have to type **/home/pungki/Documents/myfile.txt** to copy **myfile.txt** if we are in that **/home/pungki/Documents** directory. While **/home/pungki/office** is a folder where the file will be copied.
#### 2. Copy multiple files at the same time ####
To copy multiple file at the same time, we can just put the files behind the copy command which separated by space. Heres an example :
$ cp file_1.txt file_2.txt file_3.txt /home/pungki/office
![Copying multiple files](http://linoxide.com/wp-content/uploads/2014/01/cp_multiple_file.png)
#### 3. Copy a directory ####
Copying a directory is a little bit tricky. You need to add **-r** or **-R** option to do it. -r or -R option means recursive. **This option is a must** whether the directory is empty or not. Heres an example :
$ cp -r directory_1 /home/pungki/office
![Copy directory](http://linoxide.com/wp-content/uploads/2014/01/cp_directory.png)
One more thing to note is that you need to **remove the trailing slash** behind the directory name. Otherwise you will have an error message like **cp : omitting directory directory_1/**
![Copy directory error](http://linoxide.com/wp-content/uploads/2014/01/cp_directory_error.png)
If you got that error, the directory will not copied to the destination folder.
#### 4. Create hard links to files instead of copying them ####
Copying file means you must have some space on the storage to store the copied files. Sometimes for any reasons, you may want to create “shortcut” or links to the files instead of copying them. To do this, we can use **-l** option.
$ cp -l file_4.txt /home/pungki/office
![Copy hardlinks](http://linoxide.com/wp-content/uploads/2014/01/cp_hardlinks.png)
From screenshot above, we see that a hardlink of **file_4.txt** was copied into **/home/pungki/office/file_4.txt**. It marked by the same inode, **835386**. But please note, hardlinks cannot be created into directories. Lets take a look an example below.
*The original directory_1 has inode number 278230*
![Inode number of original directory](http://linoxide.com/wp-content/uploads/2014/01/cp_hardlinks_dir_1_ori.png)
*The original file_5.txt has inode number 279231*
![Original inode number of file](http://linoxide.com/wp-content/uploads/2014/01/cp_hardlinks_file_5_ori.png)
*Do cp command on directory_1*
![Copy using -rl options](http://linoxide.com/wp-content/uploads/2014/01/cp_hardlinks_rl_dir.png)
*The copied directory_1 has inode number 274800*
![Inode number of copied directory](http://linoxide.com/wp-content/uploads/2014/01/cp_hardlinks_dir_1_result.png)
*The copied file_5.txt had inode number 279231. Same with its original file*
![Inode number of copied file](http://linoxide.com/wp-content/uploads/2014/01/cp_hardlinks_file_5_result.png)
#### 5. Create symbolic links to files ####
There is another type of links called **softlinks** or **symbolic links**. We use **-s** option to do this. Heres a sample command.
$ cp -s /home/pungki/Documents/file_6.txt file_6.txt
Creating symlinks only can be done in current directory. On screenshot above, we want to create symbolic links from source directory - **/home/pungki/Documents/file_6.txt to /home/pungki/office**. But to create symbolic links, **I must inside** /home/pungki/office as a destination folder. Once I manage to be there, I can run cp **-s** command above.
Then when you list the file with detail, you will see that **/home/pungki/office/file_6.txt** is pointing to the original file. Its marked with arrow sign after the file name.
![Symbolic links](http://linoxide.com/wp-content/uploads/2014/01/cp_symlinks.png)
#### 6. Copy without following symbolic links in Source ####
To do this, we can use **-P** option. When cp command found a file with symbolic links, it will copy the as is. Take a look at the sample below.
$ cp -P file_6.txt ./movie
![Copy using -P option](http://linoxide.com/wp-content/uploads/2014/01/cp_P.png)
As you can see, the cp command will copy **file_6.txt** as is. The file type still a symbolic link.
#### 7. Copy with following symbolic links in Source ####
Now we can do this with **-L** option. Basically, **this is an opposite** of -P option above. Heres the sample.
$ cp -L file_6.txt ./movie
![Copy using -L option](http://linoxide.com/wp-content/uploads/2014/01/cp_L.png)
With this option, the copied file is the same file with the source file of **file_6.txt**. This is known from the file size. The copied file has **50 bytes** file size while the **file_6.txt** as symbolic link has **33 bytes** file size.
#### 8. Archive the files ####
When we are going to copy a directory, we will use **-r** or **-R** option. But we can also use **-a** option to archive file. This will create an **exact copy** of files and directories including symbolic links if any. Heres a sample :
$ cp -a directory_1/ /home/pungki/office
![Copy using -a option](http://linoxide.com/wp-content/uploads/2014/01/cp_a.png)
The above command will copy a directory named directory_1 into folder **/home/pungki/office**. As you can see, the **file_6.txt** still copied as symbolic links.
#### 9. Explain what is being done ####
By default, when copying activity is success, we will see a command prompt again. If you want to know what happen during the copying file, we can use **-v** option.
$ cp -v *.txt /home/pungki/office
![Verbose option](http://linoxide.com/wp-content/uploads/2014/01/cp_v.png)
When we copying all txt files in current directory to **/home/pungki/office/** directory, **-v** option will show what is being done. This additional information will make us more sure about the copying activity.
#### 10. Copy only when the source file is newer ####
To do this, we can use **-u** option. Take a look this example below.
$ cp -vu *.txt /home/pungki/office
![Copy only if newer](http://linoxide.com/wp-content/uploads/2014/01/cp_u.png)
In the beginning, we see **file_1.txt has 0 bytes** file size. Then we edit it using vi, add some content and save it. Next, we see the file size has changed into 36 bytes.
Meanwhile in **/home/pungki/office** directory, we **already have all** *.txt files. When we use -u option, combine with -v option to see what is being done, cp command will only copy a file(s) which is newer from destination directory. As the result, we see that **only file_1.txt is copied into /home/pungki/office directory**.
#### 11. Use interactive mode ####
Interactive mode will ask if the destination folder have already the file. To activate interactive mode, use **-i** option.
$ cp -ir directory_1/ /home/pungki/office/
![Interactive mode](http://linoxide.com/wp-content/uploads/2014/01/cp_i.png)
#### 12. Create backup date of each copied file ####
When the destination folder already have the file, by default cp command will overwrite the same file in the destination directory. Using **--backup** option, cp command will make a backup of each existing destination file. ../office will refer to /home/pungki/office. Heres a sample :
$ cp --backup=simple -v *.txt ../office
![Backup option](http://linoxide.com/wp-content/uploads/2014/01/cp_backup.png)
As we can see, **--backup=simple** option will create a backup file which **marked by a tilde sign (~)** at the end of the file. **--backup** option has some Control, which are :
- **none, off** : never backups (even if --backup is given)
- **numbered, t** : make numbered backups
- **existing, nil** : numbered if numbered backup exist, simple otherwise
- **simple, never** : always make simple backups
#### 13. Copy only file attributes ####
Cp command also provide us with **--attributes-only** option. As we can guess from its name, this option will only copy a file name and its attributes without copying any data. Heres a sample.
$ cp --attributes-only file_6.txt -v ../office
![Copy attributes only](http://linoxide.com/wp-content/uploads/2014/01/cp_attributes_only.png)
From screenshot above, **the original file_6.txt file has 50 bytes** file size. Using **--attributes-only** option, **the copied file will have 0 bytes** file size. This is because the content of file is not being copied.
#### 14. Force copying ####
Using **-f** option will force the copying activity. If the destination files cannot be opened, then **-f** will try again.
$ cp -f *.txt -v ../office
![Copy with force](http://linoxide.com/wp-content/uploads/2014/01/cp_f.png)
#### 15. Remove destination before copy ####
To do this, we can use **--remove-destination option**. This option is **contrast with -f option** above. If the cp command find the same file name on the destination folder, cp command will remove destination file first, the copy the new one. Heres an example.
$ cp --remove-destination *.txt -v ../office
![Remove destination option](http://linoxide.com/wp-content/uploads/2014/01/cp_remove_destination.png)
### Conclusion ###
Cp command is one of basic Linux commands. For those who want to learn Linux, must know this command. Of course you can type **man cp** or cp **--help** from your console to display its manual page to explore more detail.
--------------------------------------------------------------------------------
via: http://linoxide.com/linux-command/linux-cp-command/
译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出

View File

@ -0,0 +1,245 @@
Linux cp 命令的15个示例 - 创建文件和目录的副本*
================================================================================
拷贝文件和目录是每一个操作系统的基本指令。备份行为基本上是创建文件和目录的副本。在Linux系统下我们可以用**cp**命令来实现。
### copy 命令是什么 ###
正如我们在上文提到的,**cp**是一个用来创建文件和目录副本的命令。在这里我们提供了一些在日常操作中可能用到的cp命令的实例。
### 1. 不带任何参数下运行cp ###
这是 cp 命令最基础的使用。 拷贝名为 myfile.txt 从一个位置到另一个位置,我们可以像这样子输入:
$ cp myfile.txt /home/pungki/office
![Copy without options](http://linoxide.com/wp-content/uploads/2014/01/cp_default.png)
如果我们没有输入绝对路径,这意味着我们正在当前目录下拷贝一个文件。在上面的实例中,**myfile.txt**位于**/home/pungki/Documents**目录下。如果我们当前目录正是**/home/pungki/Documets**,那么没有必要输入**/home/pungki/Documents/myfile.txt**来拷贝文件。当**/home/pungki/office**是一个目录,则文件会拷贝到里面。
#### 2. 同时拷贝多个文件 ####
要在同时拷贝多个文件,我们只需要将多个文件用空格隔开。如下示例:
$ cp file_1.txt file_2.txt file_3.txt /home/pungki/office
![Copying multiple files](http://linoxide.com/wp-content/uploads/2014/01/cp_multiple_file.png)
#### 3. 拷贝一个目录 ####
要拷贝一个目录的话会有点棘手。你需要添加 **-r** 或者 **-R** 选项来实现。**-r**或**-R** 选项表明递归操作。无论该目录是否为空目录,**这个选项都是必要的**。如下示例:
$ cp -r directory_1 /home/pungki/office
![Copy directory](http://linoxide.com/wp-content/uploads/2014/01/cp_directory.png)
需要注意的一件事,你需要**移除在目录名尾部的斜杠**。否则你会收到类似的错误信息**cp: omitting directorydirectory_1/**
![Copy directory error](http://linoxide.com/wp-content/uploads/2014/01/cp_directory_error.png)
如果你收到错误信息,则目录不会被拷贝到目标文件夹。
### 4. 创建文件的硬链接,而不是拷贝它们####
拷贝文件意味着你必须使用一些存储空间来储存拷贝的文件。有时候出于某种原因,你可能想要创建“快捷方式”或者链接到文件,而不是拷贝它们。要做到这一点,我们可以使用**-l**选项。
$ cp -l file_4.txt /home/pungki/office
![Copy hardlinks](http://linoxide.com/wp-content/uploads/2014/01/cp_hardlinks.png)
从上图看出,我们看到**file_4.txt**的硬链接已经拷贝到**/home/pungki/office/file_4.txt**。标记有同样的 inode, **835386**。但是请注意,硬链接不能用来创建目录。下面让我们看一个例子。
*原目录 directory_1 的 inode 值是 278230*
![Inode number of original directory](http://linoxide.com/wp-content/uploads/2014/01/cp_hardlinks_dir_1_ori.png)
*原文件 file_5.txt 的 inode 值是 279231*
![Original inode number of file](http://linoxide.com/wp-content/uploads/2014/01/cp_hardlinks_file_5_ori.png)
*对 directory_1 执行 cp 命令*
![Copy using -rl options](http://linoxide.com/wp-content/uploads/2014/01/cp_hardlinks_rl_dir.png)
*拷贝的 directory_1副本的 inode 值是 274800*
![Inode number of copied directory](http://linoxide.com/wp-content/uploads/2014/01/cp_hardlinks_dir_1_result.png)
*拷贝的 file_5.txt副本的 inode 值是 279231。跟它的原文件一样*
![Inode number of copied file](http://linoxide.com/wp-content/uploads/2014/01/cp_hardlinks_file_5_result.png)
#### 5. 创建文件的符号链接 ####
也有一种链接叫做 **软链接****符号链接**。我们用 **-s** 选项来实现。下面是命令的示例。
$ cp -s /home/pungki/Documents/file_6.txt file_6.txt
创建符号链接只能在当前目录下进行。在上面的截图中,我们想要创建符号链接 **/home/pungki/office/file_6.txt 指向原文件 /home/pungki/Documents/file_6.txt**。但是为了创建符号链接,**我必须**在将/home/pungki/office作为目标目录。一旦我设法进入目录我就可以向上面一样运行 cp **-s** 命令。
现在你列出文件详情,你会看到**/home/pungki/office/file_6.txt**指向了原文件。在其文件名后标记了箭头符号。
![Symbolic links](http://linoxide.com/wp-content/uploads/2014/01/cp_symlinks.png)
#### 6. 不随符号链接拷贝原文件 #####[译注:意思是只拷贝符号链接文件]
我们可以用 **-p** 选项来实现。当对符号链接使用 cp 命令,它会照原样拷贝它自身。来看看下面的示例。
$ cp -P file_6.txt ./movie
![Copy using -P option](http://linoxide.com/wp-content/uploads/2014/01/cp_P.png)
如你所见cp 命令照原样拷贝**file_6.txt**自身。文件类型仍然是一个符号链接。
#### 7. 随符号链接拷贝原文件 ####
现在我们可以试一下**-L**选项。基本上,这个刚好与上面的 -P 选项 **相反**。下面是个示例:
$ cp -L file_6.txt ./movie
![Copy using -L option](http://linoxide.com/wp-content/uploads/2014/01/cp_L.png)
使用这个选项,拷贝的文件将会和**file_6.txt**原文件一样。我们可以从文件大小看出来。拷贝的文件有**50 字节**而当**file_6.txt**作为符号链接时文件大小只有**33 字节**。
#### 8. 文件归档 ####
When we are going to copy a directory, we will use **-r** or **-R** option. But we can also use **-a** option to archive file. This will create an **exact copy** of files and directories including symbolic links if any. Heres a sample :
当我们去拷贝一个目录时,我们会用**-r**或者**-R**选项。但是我们也可以用**-a**选项来归档文件。这样会创建文件和目录的**准确套录**,如果有的话也可以包括符号链接。下面是示例:[译注:-a 会保留原文件或目录的属性]
$ cp -a directory_1/ /home/pungki/office
![Copy using -a option](http://linoxide.com/wp-content/uploads/2014/01/cp_a.png)
上列的命令会拷贝一个名为 directory_1 的目录到**/home/pungki/office**目录下。如你所见,**file_6.txt**依然作为符号链接被复制。
#### 9. 显示正在做什么 ####
默认情况下,当拷贝作业成功时,我们仅仅会再次看到命令提示符。如果你想了解在拷贝文件时都发生了什么,我们可以用 **-v** 选项。
$ cp -v *.txt /home/pungki/office
![Verbose option](http://linoxide.com/wp-content/uploads/2014/01/cp_v.png)
当我们从当前目录下拷贝所有的 txt 文件到 **/home/pungki/office** 目录,**-v**选项会显示正在操作的过程。这些额外的信息会帮助我们了解更多拷贝过程。
#### 10. 当原文件较目标文件新时拷贝 ####
我们用 **-u**选项来实现。下面是具体示例:
$ cp -vu *.txt /home/pungki/office
![Copy only if newer](http://linoxide.com/wp-content/uploads/2014/01/cp_u.png)
起初我们看到**file_1.txt 是0字节**大小。然后我们用 vi 编辑,加入一些内容并保存。接下来,我们发现文件大小已经变为了 36 个字节。
与此同时在**/home/pungki/office**目录中,我们**已经包含了所有**txt文件。当我们用 -u 选项,结合 -v 选项来查看具体操作cp 命令会只拷贝比目标目录下新的文件。因此,我们看到**只有 file_1.txt 拷贝到 /home/pungki/office 目录下**。
#### 11. 使用交互模式 ####
交互模式下会询问是否覆盖目标目录下的文件。使用 **-i** 选项,启用交互模式。
$ cp -ir directory_1/ /home/pungki/office/
![Interactive mode](http://linoxide.com/wp-content/uploads/2014/01/cp_i.png)
#### 12. 创建备份文件 ####
当目标目录已经含有同名文件,默认情况下 cp 命令会覆盖目标目录下的同名文件。使用 **--backup** 选项cp 命令会为每一个现有的目标文件做一个备份。../office 相对于 /home/pungki/office 。下面是示例:
$ cp --backup=simple -v *.txt ../office
![Backup option](http://linoxide.com/wp-content/uploads/2014/01/cp_backup.png)
正如我们看到的,**--backup=simple** 选项会创建一个在文件名末尾用波浪符标记(~)的备份文件。**--backup** 选项也有一些其他控制:
- **none, off**:从不备份(即使给出 --backup)
- **numbered, t**:用编号备份
- **existing, nil** :如果编号备份存在则使用编号备份,否者用简易备份[也就是用波浪号]
- **simple, never** :使用简易备份
#### 13. 只拷贝文件属性 ####
cp 命令也提供给我们 **--attributes-only** 选项。顾名思义,这个选项只会拷贝文件名及其属性,不会拷贝任何数据。下面是示例:
$ cp --attributes-only file_6.txt -v ../office
![Copy attributes only](http://linoxide.com/wp-content/uploads/2014/01/cp_attributes_only.png)
从上图看出, **原文件 file_6.txt 有 50 字节**大小。用了 **--attributes-only**选项,**拷贝的文件只有0字节**大小。这是因为文件内容并没有拷贝。
### 14. 强制拷贝 #####
用了 **-f** 选项会强制进行拷贝操作。如果目标文件不能打开,可以用 **-f** 尝试一下。
$ cp -f *.txt -v ../office
![Copy with force](http://linoxide.com/wp-content/uploads/2014/01/cp_f.png)
#### 15. 在拷贝之前先删除目标 ####
我们可以用,**--remove-destination 选项** 实现。这个选项与上面的**-f选项形成对照**。如果 cp 命令在目标目录下发现同名文件, cp 命令会先删除目标文件,然后再拷贝一份新的。下面是示例:
$ cp --remove-destination *.txt -v ../office
![Remove destination option](http://linoxide.com/wp-content/uploads/2014/01/cp_remove_destination.png)
### 总结 ###
cp 命令是 Linux 下最基础的命令之一。对于那些想要学习 Linux 的人,必须得把这个命令掌握。当然你也可以在你的终端下键入 **man cp** 或者 cp **--help** 来显示更多帮助信息。
--------------------------------------------------------------------------------
via: http://linoxide.com/linux-command/linux-cp-command/
译者:[Luoxcat](https://github.com/Luoxcat) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出