终端基础：在 Linux 中复制文件和目录
终端
![](/data/attachment/album/202307/31/220802ozpf3gt9gpj2pp0f.jpg)



> 
> 在终端基础知识系列的这一部分中，学习如何在 Linux 中使用命令行复制文件和目录。
> 
> 
> 


复制文件是你经常执行的最基本但最重要的任务之一。


Linux 有一个专门的 `cp` 命令用于复制文件和目录（文件夹）。


在终端基础知识系列的这一部分中，你将学习在终端中复制文件和文件夹。



> 
> ? 回想一下，以下是你迄今为止在本终端基础知识系列中所学到的内容：
> 
> 
> * [更改目录](https://itsfoss.com/change-directories/)
> * [创建新目录](/article-15595-1.html)
> * [列出目录内容](https://itsfoss.com/list-directory-content/)
> * [创建文件](/article-15643-1.html)
> * [读取文件](https://itsfoss.com/view-file-contents/)
> * [删除文件和目录](/article-15809-1.html)
> 
> 
> 


让我们继续该系列的第七章。


### 在 Linux 命令行中复制文件


让我向你展示一些复制文件的示例。


#### 将文件复制到另一个目录


要将一个文件复制到另一目录，你所要做的就是遵循给定的命令语法：



```
cp 源文件 目标目录

```

例如，在这里，我将名为 `Hello.txt` 的文件复制到名为 `Tux` 的目录中：


![copy file to another directory in linux command line](/data/attachment/album/202307/31/220849xmp849c8l8dwa9mr.png)


正如你所看到的，文件已成功复制到 `Tux` 目录中。


#### 复制文件但重命名


你可以选择在复制文件时重命名该文件。只需为“目标文件”指定一个不同的名称即可。



```
cp 源文件 改名的文件

```

作为参考，在这里，我将名为 `Hello.txt` 的文件复制到同一目录，并将其重命名为 `Renamed_Hello.txt`：


![rename a file while copying in a same directory in linux terminal](/data/attachment/album/202307/31/220851ta35a5omcrmo8n7f.png)


为什么要这么做？ 比如说，你必须编辑配置文件。一个好的做法是在编辑配置文件之前在同一位置对其进行备份。这样，如果事情没有按计划进行，你可以恢复到旧配置。


#### 将多个文件复制到另一个位置


要将多个文件复制到另一个目录，请按以下方式执行命令：



```
cp File1 File2 File3 FileN Target_directory

```

在这里，我将多个文件复制到新位置。


![copy multiple files using the cp command in linux](/data/attachment/album/202307/31/220852qaaegi3pav0orhod.png)



> 
> ? 当你复制多个文件时，仅使用 `cp` 命令无法重命名它们。
> 
> 
> 


#### 复制时处理重复文件


默认情况下，如果目标目录中存在同名文件，`cp` 命令将覆盖该文件。


为了避免覆盖，你可以在 cp 命令中使用 `-n` 选项，它不会覆盖现有文件：



```
cp -n 源文件 目标目录

```

例如，在这里，我尝试复制目标目录中已有的两个文件，并使用 `-v` 选项来展示该命令正在执行的操作：



```
cp -n -v itsFOSS.txt LHB.txt LU.txt ~/Tux

```

![how not to override files while copying in linux using the cp command](/data/attachment/album/202307/31/220852qqz5szrqzqcuaz1f.png)


#### 交互式复制文件


但是，当你想要覆盖某些文件，而某些文件应该保持不变时该怎么办？


好吧，你可以使用 `-i` 选项在交互模式下使用 `cp` 命令，它每次都会询问你是否应该覆盖该文件：



```
cp -i 源文件 目标目录

```

![how to use cp command in interactive mode](/data/attachment/album/202307/31/220853tu6clmoll78ssz3z.png)



> 
> ?️ 自己练习上述所有示例。你已经了解如何创建文件和文件夹，因此请重新创建所有内容。
> 
> 
> 


### 在 Linux 命令行中复制目录


`mkdir` 命令用于创建新目录，`rmdir` 命令用于删除（空）目录。但没有用于复制目录的 `cpdir` 命令。


你必须使用相同的 `cp` 命令，但使用递归选项 `-r` 将目录及其所有内容复制到另一个位置：



```
cp -r 源目录 目标目录

```

例如，在这里，我将名为 `IF` 的目录复制到 `LHB`：


![how to copy a directory in linux command line](/data/attachment/album/202307/31/220854ik4zbqondmynf2y6.png)


但它复制了整个目录。?


那么，当你只想复制目录内容而不是目录本身时该怎么办？


你可以执行以下操作：


#### 仅复制目录的内容（不是目录）


要仅复制目录的内容，而不复制目录本身，请在源目录名称的末尾附加 `/.`：



```
cp -r 源目录/. 目标目录

```

在这里，我想复制名为 `IF` 的目录的内容，其中包含以下三个文件：


![check the file contents of directory using the tree command](/data/attachment/album/202307/31/220854z7bdfd7pddwkz46v.png)


我将执行以下命令将 `IF` 目录的文件内容复制到 `LHB`：



```
cp -r IF/. LHB

```

![copy the file contents of directory not a directory itself in linux command line](/data/attachment/album/202307/31/220854anfzlix1793cjcjn.png)


你还可以在此处使用 `源目录/*` 。


#### 复制多个目录


要复制多个目录，你必须按以下方式执行命令：



```
cp -r 目录1 目录2 目录3 目录N 目标目录

```

例如，在这里，我将两个名为 `IF` 和 `LU` 的目录复制到 `LHB`：



```
cp -r IF LU ~/LHB

```

![copy multiple directories using the cp command in linux command line](/data/attachment/album/202307/31/220855zqtqmzx2mpmux9ob.png)


当你想要从多个目录复制文件但不复制目录本身时，你可以执行相同的操作：



```
cp -r 目录1/. 目录2/. 目录3/. 目录N/. 目标目录

```

![copy files from multiple directories but not directories their self using the cp command](/data/attachment/album/202307/31/220856azcq9dqgcpsgzq5t.png)



> 
> ?️ 你还可以像重命名文件一样重命名目录。
> 
> 
> 


### 测试你的知识


现在，让我们看看你对到目前为止所学到的知识还记得多少。


* 创建一个名为 `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](https://itsfoss.com/author/sagar/) 选题：[lkxed](https://github.com/lkxed/) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Chapter 7: Copy Files and Directories in Linux

Learn how to copy files and directories in Linux using the command line in this part of the Terminal Basics series.

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.

-

[Change directories](https://itsfoss.com/change-directories/)

-

[Make new directories](https://itsfoss.com/make-directories/)

-

[List directory contents](https://itsfoss.com/list-directory-content/)

-

[Create files](https://itsfoss.com/create-files/)

-

[Reading files](https://itsfoss.com/view-file-contents/)

-

[Removing files and directories](https://itsfoss.com/delete-files-folders-linux/)

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](https://itsfoss.com/content/images/2023/02/copy-file-to-another-directory-in-linux-command-line.png)

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](https://itsfoss.com/content/images/2023/02/rename-a-file-while-copying-in-a-same-directory-in-linux-terminal.png)

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](https://itsfoss.com/content/images/2023/02/copy-multiple-files-using-the-cp-command-in-linux.png)

### 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](https://itsfoss.com/content/images/2023/02/how-not-to-override-files-while-copying-in-linux-using-the-cp-command.png)

### 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](https://itsfoss.com/content/images/2023/02/how-to-use-cp-command-in-interactive-mode.png)

## 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](https://itsfoss.com/content/images/2023/02/how-to-copy-a-directory-in-linux-command-line.png)

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](https://itsfoss.com/content/images/2023/02/check-the-file-contents-of-directory-using-the-tree-command.png)

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](https://itsfoss.com/content/images/2023/02/copy-the-file-contents-of-directory-not-a-directory-itself-in-linux-command-line.png)

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](https://itsfoss.com/content/images/2023/02/copy-multiple-directories-using-the-cp-command-in-linux-command-line.png)

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](https://itsfoss.com/content/images/2023/02/copy-files-from-multiple-directories-but-not-directories-their-self-using-the-cp-command.png)

## 📝 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](https://itsfoss.com/move-files-linux/).