本文介绍了重命名外壳中的多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在目录中有多个文件,例如:linux_file1.mp4linux_file2.mp4等.如何使用Shell移动这些文件,以使名称为file1.mp4file2.mp4等.我大约有30个文件要移到新名称.

I have multiple files in a directory, example: linux_file1.mp4, linux_file2.mp4 and so on. How do I move these files, using shell, so that the names are file1.mp4, file2.mp4 and so on. I have about 30 files that I want to move to the new name.

推荐答案

我喜欢 mmv 用于这种事情

mmv 'linux_*' '#1'

但是您也可以使用rename.请注意,通常有两个rename命令的语法非常不同.一种是用Perl编写的,另一种是用util-linux分发的,因此在下面将它们区分为"perl重命名"和"util重命名".

But you can also use rename. Be aware that there are commonly two rename commands with very different syntax. One is written in Perl, the other is distributed with util-linux, so I distinguish them as "perl rename" and "util rename" below.

使用Perl重命名:

rename 's/^linux_//' linux_*.mp4

正如cweiske正确指出的那样.

As cweiske correctly pointed out.

使用util重命名:

rename linux_ '' linux_*.mp4

如何知道您拥有的重命名?尝试运行rename -V;如果您的版本是util重命名,它将打印版本号;如果是perl重命名,它将无害地报告和未知选项并显示用法.

How can you tell which rename you have? Try running rename -V; if your version is util rename it will print the version number and if it is perl rename it will harmlessly report and unknown option and show usage.

如果您没有renamemmv,并且不想或无法安装它们,您仍然可以使用简单的旧shell代码来完成此操作:

If you don't have either rename or mmv and don't want to or can't install them you can still accomplish this with plain old shell code:

for file in linux_*.mp4 ; do mv "$file" "${file#linux_}" ; done

此语法将与所有符合XPG4或更高版本的POSIX sh一起使用,这实际上是当今的所有shell.

This syntax will work with any POSIX sh conforming to XPG4 or later, which is essentially all shells these days.

这篇关于重命名外壳中的多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 11:31