translated

This commit is contained in:
geekpi 2023-07-25 08:56:20 +08:00
parent a9ed558bdf
commit 06bbfbd5e9
2 changed files with 221 additions and 221 deletions

View File

@ -1,221 +0,0 @@
[#]: subject: "Linux Terminal Basics #7: Copy Files and Directories in Linux"
[#]: via: "https://itsfoss.com/copy-files-directory-linux/"
[#]: author: "Sagar Sharma https://itsfoss.com/author/sagar/"
[#]: collector: "lkxed"
[#]: translator: "geekpi"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
Linux Terminal Basics #7: Copy Files and Directories in Linux
======
![][1]
Copying files is one of the most basic yet crucial tasks you will be doing regularly.
Linux has a dedicated cp command for copying both files and directories (folders).
In this part of the Terminal Basics series, you'll learn to copy files and folders in the terminal.
> 📋 Just to recall, here's what you have learned so far in this Terminal Basics series:
> - [Change directories][2]
> - [Make new directories][3]
> - [List directory contents][4]
> - [Create files][5]
> - [Reading files][6]
> - [Removing files and directories][7]
Let's go on with the seventh chapter in the series.
### Copying files in Linux command line
Let me show you a few examples of copying files.
#### Copy a file to another directory
To copy one file to another directory, all you have to do is follow the given command syntax:
```
cp Source_file Destination_directory
```
For example, here, I have copied a file named `Hello.txt` to the directory named `Tux`:
![copy file to another directory in linux command line][8]
And as you can see, the file has successfully been copied to the Tux directory.
#### Copy the file but rename it
You can choose to rename the file while copying it. Just give a different name to the 'target file'.
```
cp Source_file Renamed_file
```
For reference, here, I have copied a file named `Hello.txt` to the same directory by renaming it to `Renamed_Hello.txt`:
![rename a file while copying in a same directory in linux terminal][9]
Why would you do that? Say, you have to edit a config file. A good practice is to make a backup of the config file in the same location before editing it. This way, you can revert to the old configuration if things don't go as planned.
#### Copy multiple files to another location
To copy multiple files to another directory, execute the command in the following fashion:
```
cp File1 File2 File3 FileN Target_directory
```
Here, I copy multiple files to a new location.
![copy multiple files using the cp command in linux][10]
> 📋 When you are copying multiple files, renaming them would not be possible with just the cp command.
#### Deal with duplicate files while copying
By default, the cp command will override the file if a file with the same name exists in the target directory.
To avoid overriding, you can use the `-n` option with the cp command, and it won't override the existing files:
```
cp -n Source_File Destination_directory
```
For example, here, I have tried to copy two files that were already there in my targeted directory and used `-v` option to showcase what is being done by the command:
```
cp -n -v itsFOSS.txt LHB.txt LU.txt ~/Tux
```
![how not to override files while copying in linux using the cp command][11]
#### Interactively copy files
But what about when you want to override some files, whereas some should be kept intact?
Well, you can use the cp command in the interactive mode using the `-i` option, and it will ask you each time whether the file should be overridden or not:
```
cp -i Source_file Destination_directory
```
![how to use cp command in interactive mode][12]
> 🖥️ Practice all the above-discussed examples yourself. You already know about creating files and folders so recreate everything.
### Copy directories in Linux command line
There is mkdir command to make new directories, rmdir to remove (empty) directories. But there is no cpdir command for copying directories.
You'll have to use the same cp command but with the recursive option `-r` to copy a directory with all its content to another location:
```
cp -r Source_dir Target_dir
```
For example, here, I have copied a directory named `IF` to `LHB`:
![how to copy a directory in linux command line][13]
But it copied the entire directory 🤨
So, what do you do when you only want to copy the directory's contents, not the directory itself?
Here's what you can do:
#### Copy only the contents of a directory (not the directory)
To copy only the contents of the directory, not the directory itself, you append `/.` at the end of the source directory's name:
```
cp -r Source_directory/. Destination_directory
```
Here, I want to copy the contents of a directory named `IF` which contains the following three files:
![check the file contents of directory using the tree command][14]
And I will execute the following command to copy the file contents of the `IF` directory to `LHB`:
```
cp -r IF/. LHB
```
![copy the file contents of directory not a directory itself in linux command line][15]
You can also use Source_directory/* here.
#### Copy multiple directories
To copy multiple directories, you will have to execute the command in the following way:
```
cp -r Dir1 Dir2 Dir3 DirN Destiniation_directory
```
For example, here, I have copied two directories named `IF` and `LU` to the `LHB`:
```
cp -r IF LU ~/LHB
```
![copy multiple directories using the cp command in linux command line][16]
You can do the same when you want to copy files from multiple directories but not the directory itself:
```
cp -r Dir1/. Dir2/. Dir3/. DirN/. Destination_directory
```
![copy files from multiple directories but not directories their self using the cp command][17]
> 🖥️ You can also rename the directories the same way you renamed files. 
### Test your knowledge
Now, let's see how much you remember the lessons learned so far.
- Create a directory called copy_practice
- Copy the file /etc/services to this newly created folder
- Create a folder named secrets under this directory and copy files /etc/passwd and /etc/services in it
- Copy the services file in copy_practice to the secrets folder but don't overwrite it
- Copy the secrets folder to your home directory
- Delete the secrets and copy_practice directories
That would give you some practice.
It's going well so far. You have learned quite a few things. In the next chapter, you'll see about moving files and folders with mv command.
--------------------------------------------------------------------------------
via: https://itsfoss.com/copy-files-directory-linux/
作者:[Sagar Sharma][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/sagar/
[b]: https://github.com/lkxed/
[1]: https://itsfoss.com/content/images/2023/03/linux-mega-packt.webp
[2]: https://itsfoss.com/change-directories/
[3]: https://itsfoss.com/make-directories/
[4]: https://itsfoss.com/list-directory-content/
[5]: https://itsfoss.com/create-files/
[6]: https://itsfoss.com/view-file-contents/
[7]: https://itsfoss.com/delete-files-folders-linux/
[8]: https://itsfoss.com/content/images/2023/02/copy-file-to-another-directory-in-linux-command-line.png
[9]: https://itsfoss.com/content/images/2023/02/rename-a-file-while-copying-in-a-same-directory-in-linux-terminal.png
[10]: https://itsfoss.com/content/images/2023/02/copy-multiple-files-using-the-cp-command-in-linux.png
[11]: https://itsfoss.com/content/images/2023/02/how-not-to-override-files-while-copying-in-linux-using-the-cp-command.png
[12]: https://itsfoss.com/content/images/2023/02/how-to-use-cp-command-in-interactive-mode.png
[13]: https://itsfoss.com/content/images/2023/02/how-to-copy-a-directory-in-linux-command-line.png
[14]: https://itsfoss.com/content/images/2023/02/check-the-file-contents-of-directory-using-the-tree-command.png
[15]: https://itsfoss.com/content/images/2023/02/copy-the-file-contents-of-directory-not-a-directory-itself-in-linux-command-line.png
[16]: https://itsfoss.com/content/images/2023/02/copy-multiple-directories-using-the-cp-command-in-linux-command-line.png
[17]: https://itsfoss.com/content/images/2023/02/copy-files-from-multiple-directories-but-not-directories-their-self-using-the-cp-command.png

View File

@ -0,0 +1,221 @@
[#]: subject: "Linux Terminal Basics #7: Copy Files and Directories in Linux"
[#]: via: "https://itsfoss.com/copy-files-directory-linux/"
[#]: author: "Sagar Sharma https://itsfoss.com/author/sagar/"
[#]: collector: "lkxed"
[#]: translator: "geekpi"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
Linux 终端基础知识 #7:在 Linux 中复制文件和目录
======
![][1]
复制文件是你经常执行的最基本但最重要的任务之一。
Linux 有一个专门的 cp 命令用于复制文件和目录(文件夹)。
在终端基础知识系列的这一部分中,你将学习在终端中复制文件和文件夹。
> 📋 回想一下,以下是你迄今为止在本终端基础知识系列中所学到的内容:
> - [更改目录][2]
> - [创建新目录][3]
> - [列出目录内容][4]
> - [创建文件][5]
> - [读取文件][6]
> - [删除文件和目录][7]
让我们继续该系列的第七章。
### 在 Linux 命令行中复制文件
让我向你展示一些复制文件的示例。
#### 将文件复制到另一个目录
要将一个文件复制到另一目录,你所要做的就是遵循给定的命令语法:
```
cp Source_file Destination_directory
```
例如,在这里,我将名为 `Hello.txt` 的文件复制到名为 `Tux` 的目录中:
![copy file to another directory in linux command line][8]
正如你所看到的,文件已成功复制到 Tux 目录中。
#### 复制文件但重命名
你可以选择在复制文件时重命名该文件。只需为“目标文件”指定一个不同的名称即可。
```
cp Source_file Renamed_file
```
作为参考,在这里,我将名为 `Hello.txt` 的文件复制到同一目录,并将其重命名为 `Renamed_Hello.txt`
![rename a file while copying in a same directory in linux terminal][9]
为什么要这么做? 比如说,你必须编辑配置文件。一个好的做法是在编辑配置文件之前在同一位置对其进行备份。这样,如果事情没有按计划进行,你可以恢复到旧配置。
#### 将多个文件复制到另一个位置
要将多个文件复制到另一个目录,请按以下方式执行命令:
```
cp File1 File2 File3 FileN Target_directory
```
在这里,我将多个文件复制到新位置。
![copy multiple files using the cp command in linux][10]
> 📋 当你复制多个文件时,仅使用 cp 命令无法重命名它们。
#### 复制时处理重复文件
默认情况下如果目标目录中存在同名文件cp 命令将覆盖该文件。
为了避免覆盖,你可以在 cp 命令中使用 `-n` 选项,它不会覆盖现有文件:
```
cp -n Source_File Destination_directory
```
例如,在这里,我尝试复制目标目录中已有的两个文件,并使用 `-v` 选项来展示该命令正在执行的操作:
```
cp -n -v itsFOSS.txt LHB.txt LU.txt ~/Tux
```
![how not to override files while copying in linux using the cp command][11]
#### 交互式复制文件
但是,当你想要覆盖某些文件,而某些文件应该保持不变时该怎么办?
好吧,你可以使用 `-i` 选项在交互模式下使用 cp 命令,它每次都会询问你是否应该覆盖该文件:
```
cp -i Source_file Destination_directory
```
![how to use cp command in interactive mode][12]
> 🖥️ 自己练习上述所有示例。你已经了解如何创建文件和文件夹,因此请重新创建所有内容。
### 在 Linux 命令行中复制目录
mkdir 命令用于创建新目录rmdir 命令用于删除(空)目录。但没有用于复制目录的 cpdir 命令。
你必须使用相同的 cp 命令,但使用递归选项 `-r` 将目录及其所有内容复制到另一个位置:
```
cp -r Source_dir Target_dir
```
例如,在这里,我将名为 `IF` 的目录复制到 `LHB`
![how to copy a directory in linux command line][13]
但它复制了整个目录。🤨
那么,当你只想复制目录内容而不是目录本身时该怎么办?
你可以执行以下操作:
#### 仅复制目录的内容(不是目录)
要仅复制目录的内容,而不复制目录本身,请在源目录名称的末尾附加 `/.`
```
cp -r Source_directory/. Destination_directory
```
在这里,我想复制名为 `IF` 的目录的内容,其中包含以下三个文件:
![check the file contents of directory using the tree command][14]
我将执行以下命令将 `IF` 目录的文件内容复制到 `LHB`
```
cp -r IF/. LHB
```
![copy the file contents of directory not a directory itself in linux command line][15]
你还可以在此处使用 Source_directory/* 。
#### 复制多个目录
要复制多个目录,你必须按以下方式执行命令:
```
cp -r Dir1 Dir2 Dir3 DirN Destiniation_directory
```
例如,在这里,我将两个名为 `IF``LU` 的目录复制到 `LHB`
```
cp -r IF LU ~/LHB
```
![copy multiple directories using the cp command in linux command line][16]
当你想要从多个目录复制文件但不复制目录本身时,你可以执行相同的操作:
```
cp -r Dir1/. Dir2/. Dir3/. DirN/. Destination_directory
```
![copy files from multiple directories but not directories their self using the cp command][17]
> 🖥️ 你还可以像重命名文件一样重命名目录。
### 测试你的知识
现在,让我们看看你对到目前为止所学到的知识还记得多少。
- 创建一个名为 copy_practice 的目录。
- 将文件 /etc/services 复制到这个新创建的文件夹。
- 在此目录下创建一个名为 secrets 的文件夹,并将文件 /etc/passwd 和 /etc/services 复制到其中。
- 将 copy_practice 中的 services 文件复制到 secrets 文件夹中,但不要覆盖它。
- 将 secrets 文件夹复制到你的主目录。
- 删除 secrets 和 copy_practice 目录。
这会给你一些练习。
到目前为止进展顺利。你已经学到了很多东西。在下一章中,你将了解如何使用 mv 命令移动文件和文件夹。
--------------------------------------------------------------------------------
via: https://itsfoss.com/copy-files-directory-linux/
作者:[Sagar Sharma][a]
选题:[lkxed][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/sagar/
[b]: https://github.com/lkxed/
[1]: https://itsfoss.com/content/images/2023/03/linux-mega-packt.webp
[2]: https://itsfoss.com/change-directories/
[3]: https://itsfoss.com/make-directories/
[4]: https://itsfoss.com/list-directory-content/
[5]: https://itsfoss.com/create-files/
[6]: https://itsfoss.com/view-file-contents/
[7]: https://itsfoss.com/delete-files-folders-linux/
[8]: https://itsfoss.com/content/images/2023/02/copy-file-to-another-directory-in-linux-command-line.png
[9]: https://itsfoss.com/content/images/2023/02/rename-a-file-while-copying-in-a-same-directory-in-linux-terminal.png
[10]: https://itsfoss.com/content/images/2023/02/copy-multiple-files-using-the-cp-command-in-linux.png
[11]: https://itsfoss.com/content/images/2023/02/how-not-to-override-files-while-copying-in-linux-using-the-cp-command.png
[12]: https://itsfoss.com/content/images/2023/02/how-to-use-cp-command-in-interactive-mode.png
[13]: https://itsfoss.com/content/images/2023/02/how-to-copy-a-directory-in-linux-command-line.png
[14]: https://itsfoss.com/content/images/2023/02/check-the-file-contents-of-directory-using-the-tree-command.png
[15]: https://itsfoss.com/content/images/2023/02/copy-the-file-contents-of-directory-not-a-directory-itself-in-linux-command-line.png
[16]: https://itsfoss.com/content/images/2023/02/copy-multiple-directories-using-the-cp-command-in-linux-command-line.png
[17]: https://itsfoss.com/content/images/2023/02/copy-files-from-multiple-directories-but-not-directories-their-self-using-the-cp-command.png