bash 获取 Perl 重命名实用程序而不是内置的重命名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22577767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:59:22  来源:igfitidea点击:

Get the Perl rename utility instead of the built-in rename

linuxperlbashrenamefile-rename

提问by WorldsEndless

Many sites (including various SO articles) talk about using "rename" using Perl expressions to rename files.

许多站点(包括各种 SO 文章)都谈到使用 Perl 表达式使用“重命名”来重命名文件。

This would be perfect, but apparently this is not the rename utility I have, and none of these articles seem to comprehend that there are multiple versions of "rename" and I can't seem to find where to get version that accepts Perl expressions.

这将是完美的,但显然这不是我拥有的重命名实用程序,而且这些文章似乎都没有理解有多个版本的“重命名”,而且我似乎无法找到接受 Perl 表达式的版本。

How can I get my hands on the more powerful rename utility mentioned here, here, and here?

我如何才能使用此处此处此处提到的更强大的重命名实用程序?

I'm running Fedora 20. My current rename command is from the util-linuxpackage and apparently I need the Perl version, which is better.

我正在运行 Fedora 20。我当前的重命名命令来自util-linux包,显然我需要 Perl 版本,它更好。

采纳答案by SzG

I can only speak for Debian. The two programs are called

我只能为 Debian 说话。这两个程序被称为

  • /usr/bin/rename.ulfrom the util-linuxpackage (hence the .ul suffix)
  • /usr/bin/prenamefrom the perlpackage
  • /usr/bin/rename.ul来自util-linux包(因此是 .ul 后缀)
  • /usr/bin/prenameperl

The actual renamecommand works via the the /etc/alternativesmechanism, whereby

实际rename命令通过/etc/alternatives机制工作,由此

  • /usr/bin/renameis a symlink to /etc/alternatives/rename
  • /etc/alternatives/renameis a symlink to /usr/bin/prename
  • /usr/bin/rename是一个符号链接 /etc/alternatives/rename
  • /etc/alternatives/rename是一个符号链接 /usr/bin/prename

The same problem has been bugging me on Cygwin, which is a Red Hat product, so should be more similar to Fedora. I'll have a look on my company laptop on Monday. And I remember the Perl-rename having worked there sometimes. Probably before I installed util-linux.

同样的问题在 Cygwin 上一直困扰着我,这是一个 Red Hat 产品,所以应该更类似于 Fedora。星期一我会看看我公司的笔记本电脑。我记得 Perl 重命名有时在那里工作过。可能在我安装之前util-linux

If you install the Perl-rename to /usr/local/binit will have precedence over renamefrom util-linux. Same goes for the manpage when installed to /usr/local/share/man/man1/.

如果您将 Perl-rename 安装到/usr/local/bin它,它将优先于renamefrom util-linux。安装到/usr/local/share/man/man1/.

I've just created a separate Perl-rename package on Github: https://github.com/subogero/rename

我刚刚在 Github 上创建了一个单独的 Perl-rename 包:https: //github.com/subogero/rename

回答by nelaaro

You can install it using cpan, which is the perl repository similar to pip for python.

您可以使用cpan安装它,这是类似于 python 的 pip 的 perl 存储库。

Here is a tutorialon using cpan.

这是有关使用 cpan的教程

If you try to run rename it it looks like this

如果您尝试运行重命名它看起来像这样

rename --help
call: rename from to files...

重命名 --help
调用:从文件重命名...

To install the perl rename you can do the following. You might need to install a few dependencies, you can generally just push enter

要安装 perl 重命名,您可以执行以下操作。您可能需要安装一些依赖项,通常只需按回车即可

cpan
cpan1> install File::Rename
CPAN: Storable loaded ok (v2.20)
Going to read '/root/.cpan/Metadata'
Database was generated on Wed, 30 Sep 2015 08:17:02 GMT
Running install for module 'File::Rename'
....
Running Build install
Installing /usr/local/share/man/man1/rename.1
Installing /usr/local/share/perl5/File/Rename.pm
Installing /usr/local/share/man/man3/File::Rename.3pm
Installing /usr/local/bin/rename
Writing /usr/local/lib64/perl5/auto/File/Rename/.packlist
RMBARKER/File-Rename-0.20.tar.gz
./Build install -- OK

cpan
cpan 1> install File::Rename
CPAN: Storable loaded ok (v2.20)
Going to read '/root/.cpan/Metadata'
Database was生成于 Wed, 30 Sep 2015 08:17:02 GMT
Running install for module 'File::Rename'
....
运行 Build install
安装 /usr/local/share/man/man1/rename.1
安装 /usr/local/share/perl5/File/Rename.pm
安装 /usr/local/share /man/man3/File::Rename.3pm
安装 /usr/local/bin/rename
写入 /usr/local/lib64/perl5/auto/File/Rename/.packlist
RMBARKER/File-Rename-0.20.tar.gz
。 /Build install -- 好的

That is how you would install the rename from cpan.
Next is to get it working on your system. As you might have more then one rename installed.

这就是您从 cpan 安装重命名的方式。
接下来是让它在你的系统上工作。因为您可能安装了多个重命名。

which rename  
/usr/bin/rename  

When you actually want this one.

当你真的想要这个的时候。

/usr/local/bin/rename --help
Usage:
    rename [ -h|-m|-V ] [ -v ] [ -n ] [ -f ] [ -e|-E *perlexpr*]*|*perlexpr*
    [ *files* ]

Options:
    -v, -verbose
            Verbose: print names of files successfully renamed.

    -n, -nono
            No action: print names of files to be renamed, but don't rename.

    -f, -force
            Over write: allow existing files to be over-written.

    -h, -help
            Help: print SYNOPSIS and OPTIONS.

    -m, -man
            Manual: print manual page.

    -V, -version
            Version: show version number.

    -e      Expression: code to act on files name.

            May be repeated to build up code (like "perl -e"). If no -e, the
            first argument is used as code.

    -E      Statement: code to act on files name, as -e but terminated by
            ';'.

I just put it into /usr/bin/but with a slight different name to make sure I did not break any existing scripts / programs the depend on the old one.

我只是将它放入/usr/bin/但名称略有不同,以确保我没有破坏任何依赖于旧脚本/程序的现有脚本/程序。

ln -s /usr/local/bin/rename /usr/bin/rename.pl

回答by qed

I had to do the following:

我必须执行以下操作:

# In bash
sudo yum install perl-CPAN
sudo cpan

# In CPAN shell
install Module::Build
install File::Rename

回答by Travis Clarke

For Debian-family (.deb) distros, I recommend @SzG's answer.

对于 Debian 系列 ( .deb) 发行版,我推荐 @SzG's answer

For RedHat-family (.rpm) distros (e.g. Fedora), if your time is precious(like mine), you can download, compile, and install, from source via cpanin one, tersecommand:

对于 RedHat-family ( .rpm) 发行版(例如 Fedora),如果您的时间很宝贵(例如我的),您可以通过cpan一个简单的命令从源代码下载、编译和安装:

# Install (replace `rename-1.9` below with another version if desired)
curl -L "http://search.cpan.org/CPAN/authors/id/P/PE/PEDERST/rename-1.9.tar.gz" | tar -xz && ( cd "rename-1.9"; perl "Makefile.PL"; make && make install )

# Cleanup
rm -rf "rename-1.9"

Note:

笔记:

INSTALL_BASEcan be set to modify the base installation directory.

e.g.perl "Makefile.PL" INSTALL_BASE=/usr/local

source

INSTALL_BASE可以设置修改基本安装目录。

例如perl "Makefile.PL" INSTALL_BASE=/usr/local

来源