I have some files and directories and I want to rename those. How can I rename them? or are there any alternative methods? Linux provides different tools and commands in order to rename files and directories. In this tutorial, we will examine mv
and rename
commands.
我有一些文件和目录,我想重命名它们。 如何重命名? 还是有其他替代方法? Linux提供了不同的工具和命令以重命名文件和目录。 在本教程中,我们将检查mv
和rename
命令。
句法 (Syntax)
The basic and most used method to rename files is using the mv command. Below you can find the mv command syntax.
重命名文件的基本方法和最常用的方法是使用mv命令。 您可以在下面找到mv命令语法。
mv OPTION OLD_FILENAME NEW_FILENAME
- OPTION is optional and can be used for different options like verbose mode, warn before the move, etc. OPTION是可选的,可用于不同的选项,例如详细模式,移动前发出警告等。
- OLD_FILENAME is the current file or folder name which will be changed to NEW_FILENAME. OLD_FILENAME是当前文件或文件夹名称,它将更改为NEW_FILENAME。
- NEW_FILENAME is the new file or folder name which will be set. NEW_FILENAME是将要设置的新文件或文件夹名称。
Linux rename file is easy. As we see the syntax is easy for simple rename operation with mv
. In this example, we will rename the file named live
into dead
.
Linux重命名文件很容易。 如我们所见,使用mv
进行简单的重命名操作很容易。 在此示例中,我们将重命名为live
的文件重命名为dead
。
$ mv live dead
重命名具有完整路径的文件 (Rename Files With Full Path)
We can use the full path while renaming files. In this example, we will rename the file named dead
into live
.
重命名文件时可以使用完整路径。 在此示例中,我们将名为dead
的文件重命名为live
。
$ mv /home/ismail/dead /home/ismail/live
列出重命名的文件 (List Renamed Files)
After renaming we may want to see files by listing them. We will use ls to list files.
重命名后,我们可能希望通过列出文件来查看文件。 我们将使用ls列出文件。
$ ls
覆盖现有文件之前询问(Ask Before Overwrite Existing File)
While moving new-file-name may already exist. In this situation mv command by default overwrites. To prevent overwrite we can provide -i option to ask before overwriting.
在移动时,新文件名可能已经存在。 在这种情况下, mv命令默认会覆盖。 为了防止覆盖,我们可以提供-i选项以在覆盖之前询问。
$ mv -i live dead
详细模式 (Verbose Mode)
While renaming we can output verbosely to during rename operation.
重命名时,我们可以在重命名操作期间详细输出到。
$ mv -v live dead
重命名命令(Rename Command)
There is a command named rename
which whole purpose is rename files and directories. Rename command has the following syntax.
有一个名为rename
的命令,其全部目的是重命名文件和目录。 重命名命令具有以下语法。
rename 'S/OLDNAME/NEWNAME/' FILES
OLDNAME
is matched oldnameOLDNAME
与旧OLDNAME
匹配NEWNAME
will be set according to OLDNAME将根据OLDNAME设置
NEWNAME
FILES
those files we want to look OLDNAMEFILES
这些文件,我们想看看OLDNAME
In this example, we will change those files who have nix
in their named into x .
在此示例中,我们会将名称中包含nix
文件更改为x。
$ rename 's/est/x/' *
指定文件和文件夹名称 (Specify File and Folder Names)
We can also specify the files and directories we want to rename. In this example, we will rename files and directories those ends with .txt
我们还可以指定我们要重命名的文件和目录。 在此示例中,我们将重命名以.txt
结尾的文件和目录。
$ rename 's/est/x/' *.txt
翻译自: https://www.poftut.com/linux-rename-files-and-folders-with-rename-command-with-examples/