且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用正则表达式重命名文件-Linux

更新时间:2023-11-23 07:50:39

就像这样:

 重命名-n's/^ file/mynewfile-/'* .txt 

或来自评论:

 重命名-n's/^ file(\ d +)/mynewfile-$ {1} -test/'* .txt___ ____^^捕获小组|被占领的团体 

在输出看起来不错时可以拖放 -n 进行重命名.

有些其他具有相同名称的工具可能会或可能不会为此,请小心.

不是 util-linux 软件包一部分的重命名命令.

如果运行以下命令( GNU )

  $重命名 

,您会看到 perlexpr ,那么这似乎是正确的工具.

如果没有,则将其设置为 Debian 上的默认值(通常已经是这种情况),并使用 Ubuntu 之类的派生词:

  $ sudo apt安装重命名$ sudo update-alternatives --set重命名/usr/bin/file-rename 


对于archlinux:

  pacman -S perl重命名 


对于RedHat系列发行版:

  yum安装名称 

"prename"软件包位于 EPEL 存储库中.


对于Gentoo:

  emerge dev-perl/重命名 


对于* BSD:

  pkg install p5-File-Rename 


对于Mac用户:

  brew安装重命名 


如果您没有此发行版的其他命令,请搜索软件包管理器以进行安装或手动进行

或者您可以使用perl CPAN:

  cpan -i File :: Rename 

旧的独立版本可以在这里找到>


重命名


此工具最初由Perl的父亲Larry Wall编写.

I have three files named

file1.txt
file2.txt
file3.txt

I am trying to rename them to

mynewfile-1.txt
mynewfile-2.txt
mynewfile-3.txt

How would I go about this using regular expressions?

Like this :

rename -n 's/^file/mynewfile-/' *.txt

or from comments :

rename -n 's/^file(\d+)/mynewfile-${1}-test/' *.txt
                   ___            ____
                    ^               ^
              capturing group       |
                               captured group

Drop -n switch when the output looks good to rename for real.

There are other tools with the same name which may or may not be able to do this, so be careful.

The rename command that is part of the util-linux package, won't.

If you run the following command (GNU)

$ rename

and you see perlexpr, then this seems to be the right tool.

If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :

$ sudo apt install rename
$ sudo update-alternatives --set rename /usr/bin/file-rename


For archlinux:

pacman -S perl-rename


For RedHat-family distros:

yum install prename

The 'prename' package is in the EPEL repository.


For Gentoo:

emerge dev-perl/rename


For *BSD:

pkg install p5-File-Rename


For Mac users:

brew install rename


If you don't have this command with another distro, search your package manager to install it or do it manually

Or you can use perl CPAN:

cpan -i File::Rename

Old standalone version can be found here


man rename


This tool was originally written by Larry Wall, the Perl's dad.