linux使用命令重命名

Fatmawati Achmad Zaenuri/Shutterstock.comFatmawati Achmad Zaenuri / Shutterstock.com

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 a fine a job, 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重命名多个文件 (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

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"

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

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

ls *.pr*

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

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

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

让我们再做一次 (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

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*

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 ‘regular expression‘ 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

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

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

删除一部分文件名 (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

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*

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

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 power of Perl is at your disposal.

rename的力量在于Perl的力量。 您可以使用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

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

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

您(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知识就可以在功能,简单性和时间上获得巨大的好处。

翻译自: https://www.howtogeek.com/423214/how-to-use-the-rename-command-on-linux/

linux使用命令重命名

linux使用命令重命名_如何在Linux上使用重命名命令相关推荐

  1. linux 文件重命名_如何在 Linux 上重命名一组文件 | Linux 中国

    要用单个命令重命名一组文件,请使用 rename 命令.它需要使用正则表达式,并且可以在开始前告诉你会有什么更改.-- Sandra Henry-stocker 几十年来,Linux 用户一直使用 m ...

  2. linux中文件重命名_如何在Linux中重命名文件?

    linux中文件重命名 This tutorial will walk you through the simple steps to rename a file in Linux. 本教程将引导您完 ...

  3. linux中更改用户密码_如何在Linux中更改用户密码

    linux中更改用户密码 In this tutorial, we will focus on how you can change a user's password in Linux. We wi ...

  4. linux中设置环境变量_如何在Linux中设置环境变量

    linux中设置环境变量 Wondering how to set environment variables in Linux? This is exactly what we'll be doin ...

  5. linux 存储映射lun 给_如何在 Linux 上扫描/检测新的 LUN 和 SCSI 磁盘 | Linux 中国

    导读:当 Linux 系统连接到 SAN(存储区域网络)后,你需要重新扫描 iSCSI 服务以发现新的 LUN.本文字数:3394,阅读时长大约:4分钟https://linux.cn/article ...

  6. linux 查看开放的端口_如何在 Linux 中查看正在使用的端口

    在对网络连接或特定于应用程序的问题进行故障排除时,首先要检查的是应该在系统上实际使用哪些端口以及哪个应用程序正在侦听特定端口.本文会介绍如何使用 netstat , ss 和 lsof 命令找出哪些服 ...

  7. linux怎么滑动命令行窗口_如何在Linux命令行界面愉快进行性能测试

    本人在做性能测试的过程中,遇到一个问题,测试机选了一台Linux服务器,只有命令行界面.执行测试用例不是非常的灵活,有时候我需要改一两个参数添加一些日志,都需要重新打包部署,虽然自动化构建比较方便,但 ...

  8. linux按目录名查找目录_如何在Linux中查找目录?

    linux按目录名查找目录 Linux provides different ways to find directories. Here we will look at how to find di ...

  9. linux如何配置网络设置_如何在Linux中配置网络

    linux如何配置网络设置 将Linux计算机连接到网络非常简单,除非不是这样. 在本文中,我讨论了基于Red Hat的Linux发行版的主要网络配置文件,并介绍了两个网络启动服务:古老的网络启动和有 ...

最新文章

  1. 聊聊服务治理中的路由设计
  2. 猪八戒网CI/CD最佳实践之路
  3. Docker(十一):Docker实战 安装 PHP 5.6、7
  4. web记录文章浏览数_内网渗透 -- 获取内网浏览器历史记录等相关信息
  5. oracle ora 16014,ORACLE ORA-16014+ORA-00312 数据库在线重做日志没有归档
  6. ArcEngine开发体验(附许可)
  7. 判断 JS 中对象的类型
  8. 【MyBatis框架】SqlMapConfig剖析
  9. Java之final详解
  10. 《Spring In Action(第4版)》阅读总结(四)渲染Web视图
  11. 嵌入式Linux开发笔试,嵌入式Linux工程师笔试题
  12. cocos2d-x 3.2 DrawNode 绘图API
  13. 数据库之间的远程操作
  14. 通过UIDocumentInteractionController预览和分享史蒂夫•乔布斯传
  15. NUC980开源项目9-官方Kernel编译
  16. PR短视频特效转场 快速画面分割视频转场过渡PR转场模板
  17. 基于微软 SAPI 的 TTS 程序实现
  18. 3d智慧城市线上3d模型展示可视化平台
  19. C++人工智能相关书籍
  20. 黑马程序员_Java基础_前期准备02-1

热门文章

  1. 巧妙下载校VOD电影
  2. Python UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xbb in position 0: invalid start byte
  3. 华为畅玩5A手机备忘录怎么同步到小米 MIX 2s?
  4. 寒假作业81-100题
  5. pgsql遇到小问题及小功能记录
  6. 关注新技术,打破自满
  7. 前端中的icon几种用法
  8. 【机器人原理与实践(二)】单目摄像头标定与单目测距
  9. oracle 定义atp规则,ATP新积分规则详解
  10. python读文件的三种方式_Python|读、写Excel文件(三种模块三种方式)