终端基础：在 Linux 中重命名文件和目录
终端 命令
![](/data/attachment/album/202311/24/215355ef7zfwuh9cx74u47.jpg)



> 
> 在这篇基本命令行教程中，你将学习在 Linux 终端重命名文件和目录的各种方法。
> 
> 
> 


如何在 Linux 终端中重命名文件和目录？你可以使用 `mv` 命令。


是的，与用于将文件和文件夹从一个位置“移动”到另一个位置的 `mv` 命令相同。


你可以在“移动文件和目录”时简单地指定文件和目录的新名称。


要重命名文件，请使用：



```
mv old_file new_file

```

同样，要重命名目录，请使用：



```
mv old_dir new_dir

```

听起来很容易，对吧？ 但我将在这里详细讨论文件重命名：


* 向你展示重命名的实际示例
* 显示通过结合 `find` 和 `exec` 命令批量重命名多个文件的示例
* 讨论用于批量重命名文件的专用程序


我们来一一看看。


### 使用 mv 命令重命名文件和目录


使用 `mv` 命令重命名同一目录中的文件：



```
mv file1.txt file2.txt

```

同样，你可以重命名同一位置的目录：



```
mv dir1 dir2

```

这是我重命名文件和目录的示例：


![](/data/attachment/album/202311/24/215501wemmamublbg7lwzz.png)


如你所见，与 [cp 命令](https://itsfoss.com/cp-command/) 不同，你不必使用递归选项来通过 [mv 命令](https://linuxhandbook.com/mv-command/) 处理目录。



> 
> ? 如果你尝试使用相同的名称重命名该文件，你会看到一个错误（显然）。
> 
> 
> 


你还可以在将文件移动到另一个位置时重命名它：



```
mv old-file-name another_dir/new-file-name

```

在下面的示例中，我将名为 `firefox-quiz.txt` 的文件移动到示例目录。在这样做的同时，我将其重命名为 `quiz.txt`。


![](/data/attachment/album/202311/24/215503krxkl7zxyr2frsrz.png)


我将其视为剪切粘贴操作。



> 
> ? 虽然你可以将多个文件移动到另一个位置（`mv file1 file2 file2 dir`），但你不能使用 `mv` 重命名多个文件。为此，你必须采用我在以下部分中讨论的其他策略。
> 
> 
> 


### 通过组合 mv、find 和 exec 命令重命名与某个模式匹配的多个文件



> 
> ? 批量重命名此类文件时要格外小心。一步错误的举动就会导致无法挽回的不良结果。
> 
> 
> 


`find` 命令用于根据文件名、类型、修改时间和其他参数在给定目录中查找文件。[exec 命令与 find](https://linuxhandbook.com/find-exec-command/) 结合使用，对 `find` 命令的结果执行命令。


使用 `find`、`exec` 和 `mv` 命令没有固定标准的结构。你可以根据需要组合它们。


假设你想通过在名称中添加 `_old` 来重命名当前目录中以 `.txt` 结尾的所有文件。所以 `file_1.txt` 变成 `file_1.txt_old` 等等。



```
find . -type f -name "*.txt" -exec mv {} {}_old ;

```

![](/data/attachment/album/202311/24/215503wuh325amr5ru55s3.png)


这只是一个示例，你的重命名要求可能会有所不同。另外，**以上仅适用于不带空格的文件名**。


**专业提示**：在处理这样的批量操作时，你可以巧妙地使用 `echo` 命令来查看将执行什么操作，而不是实际执行它。如果看起来不错，那就采取实际行动吧。


例如，首先查看哪些文件将被重命名：



```
find . -type f -name "*.txt" -exec echo mv {} {}_old \;

```

![](/data/attachment/album/202311/24/215504atnsvbs4izsvl483.png)


如你所见，实际上没有重命名任何文件。但是，如果你在没有使用回显（`echo`）命令的情况下运行上述命令，你将看到将执行什么命令。


如果你觉得没问题，请删除 `echo` 命令并继续进行实际的重命名。



```
find . -type f -name "*.txt" -exec mv {} {}_old \;

```

我在 《Efficient Linux 命令行》一书中学到了这个技巧。一本充满了这样的小宝石的优秀书。难怪它已成为 [我最喜欢的 Linux 书籍](https://itsfoss.com/best-linux-books/)之一。


### 使用 rename 命令轻松重命名多个文件


有一个名为 `rename` 的便捷命令行程序，可用于根据给定的 Perl 正则表达式模式批量重命名文件。


该实用程序不是 GNU 工具链的一部分，也不是预安装的。因此，你必须首先使用发行版的包管理器来安装它。


对于 Debian/Ubuntu，命令为：



```
sudo apt install rename

```

你可以通过以下方式使用它：



```
rename [options] perl_regex [files]

```

选项有：


* `-v` : 详细模式
* `-n` ：无操作，显示将被重命名的文件，但不重命名它们
* `-o` : 不覆盖
* `-f` : 强制覆盖现有文件
* `-s` : 不重命名软链接，只重命名其目标


现在，让我们采用你在上一节中看到的相同示例。将 `*.txt` 重命名为 `.txt_old`。



```
rename 's/\.txt$/.txt_old/' **

```

我不打算在这里解释正则表达式。`**` 表示查看所有子目录中的所有文件。


![](/data/attachment/album/202311/24/215504cbliwwlwglfjljqj.png)


正如你所看到的，它按预期工作。


### 总结


我希望你喜欢这个技巧，它可以帮助你学习在 Linux 命令行中执行基本任务。当然是针对那些想学习和使用命令行的人。桌面用户始终拥有用于此类任务的 GUI 工具。


如果你对 Linux 命令完全陌生，本系列将对你有很大帮助。



> 
> **[终端基础：Linux 终端入门](/article-16104-1.html)**
> 
> 
> 


如果你有疑问或建议，请告诉我。




---


via: <https://itsfoss.com/linux-rename-files-directories/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


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


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

How do you rename files and directories in the Linux terminal? You use the mv command.

Yes, the same mv command which is used for 'moving' files and folders from one location to another.

You can simply specify the new name for the files and directories while 'moving them'.

To rename a file, use:

`mv old_file new_file`


Similarly, to rename a directory, use:

`mv old_dir new_dir`


Sounds easy, right? But I'll discuss renaming of files in detail here:

- Show you practical examples of renaming
- Show example of bulk renaming multiple files by combining the find and exec command
- Discuss a dedicated rename utility for batch renaming files

Let's see it one by one.

## Renaming files and directories with mv command

Use the mv command to rename a file in the same directory:

`mv file1.txt file2.txt`


Similarly, you can rename a directory in the same location:

`mv dir1 dir2`


Here's an example where I rename a file and a directory:

![Renaming files and directories in Linux command line](https://itsfoss.com/content/images/2023/11/renaming-file-directory-linux-command-line.png)

As you can see, unlike the [cp command](https://itsfoss.com/cp-command/), you don't have to use the recursive option for handling directories with [mv command](https://linuxhandbook.com/mv-command/).

You may also rename a file while moving it to another location:

`mv old-file-name another_dir/new-file-name`


In the example below, I moved the file named `firefox-quiz.txt`

to the sample directory. And while doing that, I renamed it `quiz.txt`

.

![Renaming file while moving them to another location](https://itsfoss.com/content/images/2023/11/rename-file-while-moving-another-location.png)

I think of it as the cut-paste operation.

## Renaming multiple files matching a pattern by combining mv, find and exec commands

The find command is used for finding files in the given directory based on their name, type, modification time and other parameters. The [exec command is combined with find](https://linuxhandbook.com/find-exec-command/) to execute commands on the result of the find command.

There is no set, standard structure to use find, exec and mv commands. You can combine them as per your need.

Let's say you want to rename all the files ending with `.txt`

in the current directory by adding `_old`

in its name. So `file_1.txt`

becomes `file_1.txt_old`

etc.

`find . -type f -name "*.txt" -exec mv {} {}_old ;`


![bulk renaming files in Linux](https://itsfoss.com/content/images/2023/11/bulk-renaming-files-linux-1.png)

This is just an example and your renaming requirements could be different. Also, **the above works with filenames without spaces only**.

**Pro Tip**: When dealing with bulk actions like this, you can smartly use the echo command to see what action will be performed instead of actually performing it. If it looks alright, then go with the actual action.

For example, first see what files will be renamed:

`find . -type f -name "*.txt" -exec echo mv {} {}_old \;`


![Dry run bulk file renaming with echo command](https://itsfoss.com/content/images/2023/11/use-echo-for-dry-run-renaming-files.png)

As you can see, no files were actually renamed. But you get to see what command will be the action if you run the above command without echo.

If it looks alright to you, remove the echo command and proceed with actual renaming.

`find . -type f -name "*.txt" -exec mv {} {}_old \;`


I learned this trick in the Efficient Linux at the Command Line book. An excellent book filled with small gems like this. No wonder it has become one of [my favorite Linux books](https://itsfoss.com/best-linux-books/).

![](https://itsfoss.com/content/images/2023/04/efficient-at-linux-command-line-horizontal.png)

#### New Book: Efficient Linux at the Command Line

Pretty amazing Linux book with lots of practical tips. It fills in the gap, even for experienced Linux users. Must have in your collection.

[Get it from Amazon](https://amzn.to/3MPjiHw)

## Renaming multiple files easily with the rename command

There is a handy command line utility called rename which could be used for batch renaming files based on the given Perl regex pattern.

This utility is not party of GNU toolchain and neither it comes preinstalled. So you have to use your distribution's package manager to install it first.

For Debian/Ubuntu, the command would be:

`sudo apt install rename`


You can use it in the following manner:

`rename [options] perl_regex [files]`


The options are:

- -v : Verbose mode
- -n : No action, show the files that would be renamed but don’t rename them
- -o : No overwrite
- -f : Force overwrite existing files
- -s : Don't rename the soft link but its target

Now, let's take the same example that you saw in the previous section. Renaming the *.txt to .txt_old.

`rename 's/\.txt$/.txt_old/' **`


I am not going to explain the regex here. The `**`

means to look into all files in all subdirectories.

![Use rename command in Linux](https://itsfoss.com/content/images/2023/11/use-rename-command-linux.png)

And as you can see, it works as expected.

[Read more](https://oylenshpeegul.typepad.com/blog/2011/12/a-tale-of-two-renames.html)about it.

## Conclusion

I hope you liked this tip that helps you learn to do basic tasks in the Linux command line. Of course, it is for those who want to learn and use the command line. Desktop users always have the GUI tools for such tasks.

If you are absolutely new to Linux commands, this series will help you a great deal.

[Getting Started With Linux TerminalWant to know the basics of the Linux command line? Here’s a tutorial series with a hands-on approach.](https://itsfoss.com/linux-terminal-basics/)![](https://itsfoss.com/content/images/2023/07/getting-started-with-linux-terminal.png)


![](https://itsfoss.com/content/images/2023/07/getting-started-with-linux-terminal.png)

Let me know if you have questions or suggestions.