Bash – How to batch rename files by removing special character without using rename in linux shell

bashmv

For example, I have 100 files and their names all have blank space. One of them is "The Monkey King Return (2015).mkv".

How to remove all blank in name and replace with dot character, also remove "(" and ")"?

The result should be "The.Monkey.King.Return.2015.mkv".

how I can do this in batch?

I am actually running shell script in my Synology NAS via ssh, which is a BusyBox Linux distribution with bash and ash installed, without gcc. Already tried for serveral days, cannot figure it out properly.

Tools aviable: mv / xargs / sed / awk / other standard linux cmd.

Also how about recursively do the renaming for sub folders as well?

Edit: just installed apt-get and rename cmd into my synology nas by using Debian Chroot from https://synocommunity.com/, so it is ok now.

To make person who need simple answer, the cmd is:

find . -iname \*\ \*.\*|rename 's/\ /\./g'

Best Answer

Most linux distro's include util-linux which gives you the rename command. You can do what you request by running these commands:

rename ' ' _ *.mkv
rename '(20' 20 *.mkv
rename ').' . *.mkv

You can experiment with the options, it's pretty flexible. If you want to do this recursively, combine rename with the find command like this:

find . -type f -name \*.mkv -exec rename ' ' _ {} \;

These are explained in the manual; type man find or man rename to read them.


Beware that some distro's apparently include a different command which accepts perl regular expressions. If yours does too, you'll need a slightly different syntax:

rename 's/\ /./g' *.mkv
rename 's/[\)\(]//g' *.mkv
Related Question