Get the Perl rename utility instead of the built-in rename [closed]
Asked Answered
H

8

36

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 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.

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-linux package and apparently I need the Perl version, which is better.

Harrar answered 22/3, 2014 at 12:44 Comment(5)
If you have perl, it should be available as prename too.Ushas
@Ushas I have perl, but man prename, prename, and sudo yum search prename all come back empty handed.Harrar
The script itself can be found at perlmonks.org/?node_id=303814Gerstner
I've downloaded the script, fixed inserted linebreaks, and used it; it works correctly. I'd be willing to mark 'answered' if someone could put a clean download (is there one with a man page?) and how to use it globally instead of writing my own alias to hit a perl file I've stuck somewhere.Harrar
In case you don't have root access in your machine, here is a gist with the script (necessary linebreaks fixed, as mentioned by @WorldsEndless): gist.github.com/rocarvaj/6abf7dd1e083963a596430ac43f88e34Spokane
C
22

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

Conciliator answered 22/3, 2014 at 14:7 Comment(4)
Looks great. If you'll just add a line I think the answer will be complete: how to install your git package (just a line about "sudo make"). Thanks!Harrar
Re: Cygwin, Red Hat own the copyright on the core cygwin1.dll but pays only one full-time developer to work on Cygwin, and she (Corinna Vinschen) doesn't work on Cygwin Perl. The vast majority of Cygwin work is done by community volunteers, and they are not constrained in making decisions to match Fedora. Cygwin Perl is currently maintained by several people, none of whom get paid by Red Hat to work on Cygwin. (Eric Blake, a Red Hat employee, maintains the perl-error package, but he's publicly stated that his Cygwin work is off the clock.)Negress
That's amazing. And Cygwin's been the most reliable piece of software on Windows for ages. :-)Conciliator
If you wanted to contribute a perl-rename package, I'd support it. You'll notice my name on the Cygwin package maintainer list, too. And no, I don't want to create that package. My personal choice for fancy renaming – when Bash one-liners aren't appropriate — is mmv and I hardly ever need to use it.Negress
A
19

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
Admixture answered 30/9, 2015 at 9:14 Comment(4)
For my case, i need sudo cpan.Iquique
Even sudo cpan and the install didn't give me this script. Fedora 24. Is it because I already installed the GitHub answer? I had uninstalled that, and also directly referenced /usr/bin/rename, but didn't get this 'super-powered' version.Unknowing
@Unknowing It didn't install rename for me, either. It did install a file-rename that seems to be the same program. EDIT: I'm using Strawberry Perl.Hurty
Ran this on openSuse and it replaced the existing /usr/bin/rename which is not what I wanted, I wanted to have both. Any idea how to get the old one back?Wagram
J
5

On RedHat 8.4 (but may work on Fedora)

sudo yum install perl-CPAN
sudo cpan
install module::Build
install File::Rename

than you can create an alias:

alias prename='/usr/local/bin/rename'

An example:

touch pic.jpeg
prename 's/\.jpeg$/.jpg/' *.jpeg

Here you create an empty file with the .jpeg extension. The next line renames all the files with .jpeg extension in the current directory: it removes the e from the jpeg extension of these files. (The first line is there to be sure that there is at least one file like that in the directory.)

(On Debian/Ubuntu you can install the rename package, then you'll have the rename command same as the prename above. Also read SzG's answer about alternatives and symbolic links: it may or may not be as simple as I wrote.

Alternatively you need to install the libpath-tiny-perl package to install CPAN.)

Josettejosey answered 5/7, 2021 at 17:18 Comment(0)
A
4

I had to do the following:

# In bash
sudo yum install perl-CPAN
sudo cpan

# In CPAN shell
install Module::Build
install File::Rename
Astroid answered 9/8, 2017 at 9:27 Comment(0)
S
2

For Arch Linux, its

sudo pacman -S perl-rename
Squamation answered 29/12, 2021 at 17:23 Comment(0)
A
2

I created a post about Perl's rename for many distro:

https://unix.stackexchange.com/a/727288/12574

  • rpm based distros:
    • dnf install prename
  • archlinux:
    • pacman -S perl-rename
  • *BSD:
    • pkg install p5-File-Rename
  • Debian like/Ubuntu
    • apt install rename
  • slackware:
Alphaalphabet answered 22/12, 2022 at 10:36 Comment(0)
M
1

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

Matchmark answered 18/12, 2017 at 23:9 Comment(1)
For RedHat-family distros you should be able to just yum install prename. The 'prename' package is in the EPEL repository.Klopstock
M
-1

I recently had to install the glorious Perl rename package to Alpine Linux in a Docker container for a Gitlab CI/CD operation:

apk update
apk add --no-cache make perl-utils
cpan File::Rename
Marciano answered 13/2, 2023 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.