Linux – Rename multiple files to remove common suffix from filenames

command linelinuxUbuntu

I have a directory (Linux user) with a number of files which contain an added [!] to the end of each file name so that each file reads out as:

  • foo something [!].zip
  • bar something [!].zip
  • helloworld [!].zip
  • etc.

What is the quickest way to batch rename these to remove the ending [!] character combination from these file names?

Best Answer

You can use the rename tool like this

rename 's/\ \[\!\]//' *.zip

I recommend that you first add the -n switch, which causes rename to just show what it will do, but not rename any files.

rename -n 's/\ \[\!\]//' *.zip

Note that this will only remove the first occurence of [!] in the filename, only if it is preceded by a space, and regardless of whether it is at the end of the filename or not.


basic syntax of rename:

rename 's/from/to/' filenames

will replace from with to in all filenames.

here is a detailed explanation: https://www.computerhope.com/unix/rename.htm


note there are two rename commands: one from the util-linux package and one from the perl package. the command i refer to above is the perl variant. the perl variant is much more popular and useful. usually when people talk about rename they mean the perl variant.

in debian (and therefore ubuntu) rename is the perl variant by default. https://stackoverflow.com/questions/22577767/get-the-perl-rename-utility-instead-of-the-built-in-rename

in arch linux rename is the util-linux variant while perl-rename is the perl variant. https://bbs.archlinux.org/viewtopic.php?id=85128

here is how you can see what version you are using:

$ rename --version
rename from util-linux 2.30.1
$ perl-rename --version
perl-rename 1.9
Related Question