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

前端 未结 4 548
离开以前
离开以前 2020-11-27 05:28

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

This would be perfect, but apparently this is not the

相关标签:
4条回答
  • 2020-11-27 05:48

    I had to do the following:

    # In bash
    sudo yum install perl-CPAN
    sudo cpan
    
    # In CPAN shell
    install Module::Build
    install File::Rename
    
    0 讨论(0)
  • 2020-11-27 05:58

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

    • /usr/bin/rename.ul from the util-linux package (hence the .ul suffix)
    • /usr/bin/prename from the perl package

    The actual rename command works via the /etc/alternatives mechanism, whereby

    • /usr/bin/rename is a symlink to /etc/alternatives/rename
    • /etc/alternatives/rename is a symlink to /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.

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

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

    0 讨论(0)
  • 2020-11-27 06:01

    For Debian-family (.deb) distros, I recommend @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 cpan in one, terse command:

    # 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_BASE can be set to modify the base installation directory.

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

    source

    0 讨论(0)
  • 2020-11-27 06:08

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

    Here is a tutorial on using cpan.

    If you try to run rename it it looks like this

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

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

    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

    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.

    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.

    ln -s /usr/local/bin/rename /usr/bin/rename.pl
    
    0 讨论(0)
提交回复
热议问题