博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux使用命令重命名_如何在Linux上使用重命名命令
阅读量:2521 次
发布时间:2019-05-11

本文共 14053 字,大约阅读时间需要 46 分钟。

linux使用命令重命名

linux使用命令重命名

Linux laptop showing a bash prompt

Get to grips with the file renaming powerhouse of the Linux world and give mv—and yourself—a rest. Rename is flexible, fast, and sometimes even easier.  Here’s a tutorial to this powerhouse of a command.

掌握Linux世界的文件重命名功能,让mv和您自己放松一下。 Rename是灵活,快速的,有时甚至更容易。 这是这个强大的命令教程。

mv怎么了? (What’s Wrong With mv?)

There’s nothing wrong with mv . The command does , and it is found on all Linux distributions, in macOS, and in other Unix-like operating systems. So it’s always available. But sometimes you just need a bulldozer, not a shovel.

mv没错。 该命令可以 ,并且可以在所有Linux发行版,macOS和其他类似Unix的操作系统中找到该命令。 因此它始终可用。 但是有时候您只需要推土机,而不是铲子。

The mv command has a purpose in life, and that is to move files. It is a happy side effect that it can be used to move an existing file into a new file, with a new name. The net effect is to rename the file, so we get what we want. But mv is not a dedicated file renaming tool.

mv命令的作用是实现生命,即移动文件。 令人高兴的是,它可用于将现有文件移动新文件中并使用新名称。 最终结果是重命名文件,因此我们得到了想要的东西。 但是mv不是专用的文件重命名工具。

用mv重命名单个文件 (Renaming a Single File With mv)

To use mv to rename a file type mv, a space, the name of the file, a space, and the new name you wish the file to have. Then press Enter.

要使用mv重命名文件类型,请输入mv ,空格,文件名,空格以及希望文件具有的新名称。 然后按Enter。

You can use ls to check the file has been renamed.

您可以使用ls检查文件是否已重命名。

mv oldfile.txt newfile.txt
ls *.txt
mv oldfile.txt newfile.txt in a terminal window

用mv重命名多个文件 (Renaming Multiple Files with mv)

Things get trickier when you want to rename multiple files. mv has no capability to deal with renaming multiple files. You must resort to using some nifty Bash tricks. That’s fine if you know some medium-grade command-line fu, but the complexity of renaming multiple files with mv stands in stark contrast to the ease of using mv to rename a single file.

要重命名多个文件时,事情变得棘手。 mv无法处理重命名多个文件的功能。 您必须使用一些漂亮的Bash技巧。 如果您知道一些中级命令行功能,那很好,但是使用mv重命名多个文件的复杂性与使用mv重命名单个文件的简便性形成了鲜明的对比。

Things escalate quickly.

事情Swift升级。

Let’s say we’ve got a directory with a variety of files in it, of differing types. Some of these files have a “.prog” extension. We want to rename them at the command line so that they have a “.prg” extension.

假设我们有一个目录,其中包含各种类型的文件。 其中一些文件的扩展名为“ .prog”。 我们要在命令行中重命名它们,以便它们具有“ .prg”扩展名。

How do we wrangle mv into doing that for us? Let’s take a look at the files.

我们如何纠缠mv为我们做到这一点? 让我们看一下文件。

ls *.prog -l
ls *.prog -l in a terminal window

Here’s one way to do it that doesn’t resort to writing an actual Bash script file.

这是一种无需编写实际Bash脚本文件的方法。

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"
for f in *.prog; do mv -- "$f" "${f%.prog}.prg" in a terminal window

DId that work? Let’s check the files and see.

努力工作吗? 让我们检查文件看看。

ls *.pr*
ls *.pr* in a terminal window

So, yes, it worked. They’re all “.prg” files now, and there are no “.prog” files in the directory.

所以,是的,它起作用了。 它们现在都是“ .prg”文件,目录中没有“ .prog”文件。

刚才发生了什么? (What Just Happened?)

What did that long command actually do? Let’s break it down.

那条长命令实际上是做什么的? 让我们分解一下。

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"

The first part starts a loop that is going to process each “.prog” file in the directory, in turn.

第一部分开始一个循环,该循环将依次处理目录中的每个“ .prog”文件。

The next part says what the processing will do. It is using mv to move each file to a new file. The new file is going to be named with the original file’s name excluding the  “.prog” part. A new extension of “.prg” will be used instead.

下一部分说明处理将要执行的操作 。 它使用mv将每个文件移动到新文件。 新文件将使用原始文件名(“ .prog”部分除外)命名。 将会使用新的扩展名“ .prg”。

必须有一个更简单的方法 (There Has to be a Simpler Way)

Most definitely. It is the rename command.

明确地。 这是rename命令。

rename is not part of a standard Linux distribution, so you will need to install it. It also has a different name in different families of Linux, but they all work the same way. You’ll just have to substitute the appropriate command name according to the Linux flavor you’re using.

rename不是标准Linux发行版的一部分,因此您需要安装它。 在不同的Linux系列中,它的名称也不同,但是它们的工作方式相同。 您只需要根据所使用的Linux版本替换适当的命令名称即可。

in Ubuntu and Debian-derived distributions you install rename like this:

在Ubuntu和Debian发行版中,您可以按以下方式安装rename

sudo apt-get install rename
sudo apt-get install rename in a terminal window

In Fedora and RedHat-derived distributions you install prename like this. Note the initial “p,” which stands for Perl.

在Fedora和RedHat派生的发行版中,您需要安装这样的prename 。 注意最初的“ p”,代表Perl。

sudo dnf install prename
sudo dnf install prename in a terminal window

To install it in Manjaro Linux use the following command. Note that the renaming command is called perl-rename.

要将其安装在Manjaro Linux中,请使用以下命令。 请注意,重命名命令称为perl-rename

sudo pacman -Syu perl-rename
sudo pacman -Syu perl-rename in a terminal window

让我们再做一次 (Let’s Do That Again)

And this time we’ll use rename. We’ll roll back the clock so that we have a set of “.prog” files.

这次我们将使用rename 。 我们将回滚时钟,以便获得一组“ .prog”文件。

ls *.prog
ls *.prog in a terminal window

Now let’s use the following command to rename them. We’ll then check with ls whether it worked. Remember to substitute rename with the appropriate command name for your Linux if you’re not using Ubuntu or a Debian-derived Linux.

现在,让我们使用以下命令来重命名它们。 然后,我们将与ls检查是否有效。 如果您不使用Ubuntu或Debian衍生的Linux,请记住用适合您的Linux的命令名替换rename

rename 's/.prog/.prg/' *.prog
ls *.pr*
rename 's/.prog/.prg/' *.prog in a terminal window

That worked, they’re now all “.prg” files, and there are no “.prog” files left in the directory.

没问题,它们现在都是“ .prg”文件,目录中没有“ .prog”文件。

这段时间发生了什么? (What Happened This TIme?)

Let’s explain that bit of magic, in three parts.

让我们分三部分来说明这一点魔术。

The first part is the command name, rename (or prename or perl-rename , for the other distributions).

第一部分是命令名称, rename (对于其他发行版,则为prenameperl-rename )。

The last part is *.prog, which tells rename to operate on all “.prog” files.

最后一部分是*.prog ,它告诉rename对所有“ .prog”文件进行操作。

The middle part defines the work we want to be done on each filename. The s means substitute. The first term (.prog) is what rename will search for in each filename and the second term (.prg)  is what it will be substituted with.

中间部分定义了我们要对每个文件名完成的工作。 s表示替代。 第一个术语( .prog )是rename将在每个文件名中搜索的内容,第二个术语( .prg )是它将被替换的名称。

The middle part of the command, or central expression, is a Perl ‘‘ and it is what gives the rename command its flexibility.

该命令的中间部分(即中央表达式)是Perl的“ ”,正是它使rename命令具有灵活性。

更改文件名的其他部分 (Changing Other Parts of a Filename)

We’ve changed filename extensions so far, let’s amend other parts of the filenames.

到目前为止,我们已经更改了文件扩展名,让我们修改文件名的其他部分。

In the directory are a lot of C source code files. All of the filenames are prefixed with “slang_”. We can check this with ls.

目录中有很多C源代码文件。 所有文件名都以“ slang_”为前缀。 我们可以用ls检查。

ls sl*.c
ls sl*.c in a terminal window

We are going to replace all occurrences of “slang_” with “sl_”. The format of the command is already familiar to us. We’re just changing the search term, the replacement term, and the file type.

我们将用“ sl_”替换所有出现的“ slang_”。 该命令的格式已经为我们所熟悉。 我们只是在更改搜索词,替换词和文件类型。

rename 's/slang_/sl_' *.c
rename 's/slang_/sl_' *.c in a terminal window

This time we are looking for “.c” files, and searching for “slang_”. Whenever “slang_” is found in a filename it is substituted with “sl_”.

这次我们正在寻找“ .c”文件,并寻找“ slang_”。 只要在文件名中找到“ slang_”,就会将其替换为“ sl_”。

We can check the result of that command by repeating the ls command from above with the same parameters:

我们可以通过使用相同的参数从上方重复ls命令来检查该命令的结果:

ls sl*.c
ls sl*.c in a terminal window

删除一部分文件名 (Deleting Part of a Filename)

We can remove a part of a filename by substituting the search term with nothing.

我们可以通过不添加任何搜索词来删除文件名的一部分。

ls *.c
rename 's/sl_//' *.c
ls *.c
rename 's/sl_//' *.c in a terminal window

We can see from the ls command that our “.c” files are all prepended with “sl_”. Let’s get rid of that altogether.

ls命令可以看到,我们的“ .c”文件都以“ sl_”开头。 让我们完全摆脱它。

The rename command follows the same format as before. We’re going to be looking for “.c” files. The search term is “sl_”, but there is no substitution term. Two backslashes without anything between them means nothing, an empty string.

rename命令的格式与以前相同。 我们将寻找“ .c”文件。 搜索项是“ sl_”,但没有替代项。 两个反斜杠之间没有任何含义,表示空字符串。

rename will process each “.c” file in turn. It will search for “sl_” in the filename. If it is found, it will be replaced by nothing. In other words, the search term is deleted.

rename将依次处理每个“ .c”文件。 它将在文件名中搜索“ sl_”。 如果找到它,它将什么也不会替换。 换句话说,搜索项被删除。

The second use of the ls command confirms that the “sl_” prefix has been removed from every “.c” file.

ls命令的第二次使用确认已从每个“ .c”文件中删除“ sl_”前缀。

将更改限制在文件名的特定部分 (Limit Changes to Specific Parts of Filenames)

Let’s use ls to look at files that have the string “param” in their filename. Then we’ll use rename to replace that string with the string “parameter”. We’ll use ls once more to see the effect the rename command has had on those files.

让我们使用ls来查看文件名中包含字符串“ param”的文件。 然后,我们将使用rename将字符串替换为字符串“ parameter”。 我们将再次使用ls来查看rename命令对这些文件的影响。

ls *param*
rename 's/param/parameter' *.c
ls *param*
rename 's/param/parameter' *.c in a terminal window

Four files are found that have “param” in their filename. param.c, param_one.c, and param_two.c all have “param” at the start of their name. third_param.c has “param” at the end of its name, just before the extension.

找到四个文件名中带有“ param”的文件。 param.c,param_one.c和param_two.c都有“参数”,在他们的名字开始 。 third_param.c在其名称的末尾 ,即扩展名之前,具有“ param”。

The rename command is going to search for “param” everywhere in the filename, and replace it with “parameter” in all cases.

rename命令将在文件名中的所有位置搜索“ param”,并在所有情况下均将其替换为“ parameter”。

The second use of the ls command shows us that that is exactly what has happened. Whether “param” was at the start or at the end of the filename, it has been replaced by “parameter.”

ls命令的第二次使用向我们展示了这确实发生了。 无论“ param”是在文件名的开头还是结尾,都已被“ parameter”代替。

We can use Perl’s metacharacters to refine the behavior of the middle expression. Metacharacters are symbols that represent positions or sequences of characters. For example, ^ means “start of a string,” $ means “end of a string,” and . means any single character (apart from a newline character).

我们可以使用Perl的元字符来完善中间表达式的行为。 元字符是代表字符位置或序列的符号。 例如, ^表示“字符串的开始”, $表示“字符串的结束”,和. 表示任何单个字符(除了换行符)。

We’re going to use the start of string metacharacter ( ^ ) to restrict our search to the start of the filenames.

我们将使用字符串元字符( ^ )的开头将搜索限制为文件名的开头。

ls *param*.c
rename 's/^parameter/value/' *.c
ls *param*.c
ls value*.c
rename 's/^parameter/value/' *.c in a terminal window

The files we renamed earlier are listed, and we can see the string “parameter” is at the start of three filenames and it is at the end of one of the filenames.

列出了我们之前重命名的文件,我们可以看到字符串“ parameter”在三个文件名的开头,并且在其中一个文件名的结尾。

Our rename command uses the start of line (^)  metacharacter before the search term “parameter.” This tells rename to only consider the search term to have been found if it is at the start of the filename. The search string “parameter” will be ignored if it is anywhere else in the filename.

我们的rename命令使用搜索词“参数”之前的行首(^ )元字符。 这告诉rename仅在文件名开头才考虑已找到搜索词。 如果搜索字符串“参数”在文件名中的其他位置,则将被忽略。

Checking with ls, we can see that the filename that had “parameter” at the end of the filename has not been modified, but the three filenames that had “parameter” at the start of their names have had the search string replaced by the substitute term “value.”

使用ls检查,我们可以看到文件名末尾带有“ parameter”的文件名没有被修改,但是名字开头有“ parameter”的三个文件名已经用替换字符串替换了术语“价值”。

The power of rename lies in the power of Perl. All of the is at your disposal.

rename的力量在于Perl的力量。 您可以使用所有功能。

搜索分组 (Searching With Groupings)

rename has yet more tricks up its sleeve. Let’s consider the case where you might have files with similar strings in their names. They’re not exactly the same strings, so a simple search and substitution won’t work here.

rename还有很多技巧。 让我们考虑一下您的文件名中包含相似字符串的情况。 它们不是完全相同的字符串,因此在这里无法进行简单的搜索和替换。

In this example we use ls to check which files we have that start with “str”. There are two of them, string.c and strangle.c. We can rename both strings at once using a technique called grouping.

在此示例中,我们使用ls检查以“ str”开头的文件。 它们有两个,string.c和strangle.c。 我们可以使用称为分组的技术一次重命名两个字符串。

The central expression of this rename command will search for strings within filenames that have the character sequence “stri” or “stra” where those sequences are immediately followed by “ng”. In other words, our search term is going to look for “string” and “strang”. The substitution term is “bang”.

rename命令的中心表达式将在文件名中搜索字符串,该文件名应具有字符序列“ stri” “ stra”,其中这些序列后跟“ ng”。 换句话说,我们的搜索词将查找“ string” “ strang”。 替代术语是“爆炸”。

ls str*.c
rename 's/(stri|stra)ng/bang/' *.c
ls ban*.c
rename 's/(stri|stra)ng/bang/' *.c in a terminal window

Using ls a second time confirms that string.c has become bang.c and strangle.c is now bangle.c.

第二次使用ls确认string.c变为bang.c,而strangle.c现在变为bangle.c。

使用带重命名的翻译 (Using Translations With rename)

The rename command can perform actions on filenames called translations. A simple example of a translation would be to force a set of filenames into uppercase.

rename命令可以对称为翻译的文件名执行操作。 转换的一个简单示例是将一组文件名强制为大写。

In the rename command below notice that we’re not using an s/ to start the central expression, we’re using y/. This tells rename we’re not performing a substitution; we’re performing a translation.

在下面的rename命令中,请注意,我们没有使用s/来启动中央表达式,而是使用了y/ 。 这说明rename我们不执行替换; 我们正在翻译。

The a-z term is a Perl expression that means all lowercase characters in the sequence from a to z. Similarly, the A-Z term represents all uppercase letters in the sequence from A to Z.

az术语是Perl表达式,表示从a到z序列中的所有小写字符。 同样, AZ项代表从A到Z的序列中的所有大写字母。

The central expression in this command could be paraphrased as “if any of the lowercase letters from a to z are found in the filename, replace them with the corresponding characters from the sequence of uppercase characters from A to Z.”

该命令的中心表达式可以解释为“如果在文件名中找到了从a到z的任何小写字母,请用从A到Z的大写字符序列中的相应字符替换它们。”

To force the filenames of all “.prg” files to uppercase, use this command:

要强制所有“ .prg”文件的文件名都使用大写,请使用以下命令:

rename ‘y/a-z/A-Z/’ *.prg

重命名'y / az / AZ /'* .prg

ls *.PRG
rename 'y/a-z/A-Z/' *.prg in a terminal window

The ls command shows us that all of the “.prg” filenames are now in uppercase. In fact, to be strictly accurate, they’re not “.prg” files anymore. They’re “.PRG” files. Linux is case sensitive.

ls命令向我们显示所有“ .prg”文件名现在都是大写的。 实际上,严格来说,它们不再是“ .prg”文件。 它们是“ .PRG”文件。 Linux区分大小写。

We can reverse that last command by reversing the position of the a-z and A-Z terms in the central expression.

我们可以通过反转中央表达式中azAZ项的位置来反转最后一个命令。

rename ‘y/A-Z/a-z/’ *.PRG

重命名'y / AZ / az /'* .PRG

ls *.prg
rename 'y/A-Z/a-z/' *.PRG in a terminal window

您(Wo | Do)五分钟不学习Perl (You (Wo|Do)n’t Learn Perl in Five Minutes)

Getting to grips with Perl is time well spent. But to start using the time-saving capabilities of the rename command, you don’t need to have much Perl knowledge at all to reap large benefits in power, simplicity and time.

熟悉Perl是花费时间。 但是,要开始使用rename命令的省时功能,您根本不需要具备很多Perl知识就可以在功能,简单性和时间上获得巨大的好处。

翻译自:

linux使用命令重命名

转载地址:http://dfuwd.baihongyu.com/

你可能感兴趣的文章
各种标准文档格式的解析工具
查看>>
iOS 8 通知设置页找不到App的问题
查看>>
new Date()的浏览器兼容性问题
查看>>
小技巧1-最大差
查看>>
dp、sp 、 px之间的相互转化的工具类
查看>>
资源推荐 五个常用MySQL图形化管理工具
查看>>
使用WebService与Oracle EBS进行集成
查看>>
Know How And When To Use System.Message_Level To Control Messages In Oracle Forms
查看>>
python 常用库
查看>>
EBS R12.1安装中文补丁包BUG:FAILED: file XLIFFLoader.class on worker [X]
查看>>
类变量
查看>>
git 常用命令总结
查看>>
luogu3769 【模板】AC自动机(加强版)
查看>>
ubuntu Apache安装设置
查看>>
UINavigationController导航栏的隐藏和显示(转)
查看>>
curl 常用命令
查看>>
为啥用ip不可以访问知乎,而百度却可以?
查看>>
Winform 根据Point截图并保存到指定路径
查看>>
HDU 6038.Function-数学+思维 (2017 Multi-University Training Contest - Team 1 1006)
查看>>
Tornado-第一篇-搭建网页
查看>>