Renaming multiple files at once with a pattern on Ubuntu

Posted on

Problem :

I have around 300 files named

I need to rename them all to

How can i do this?

Solution :

You can do this with the rename command line utility. To do what you want you need a simple regular expression:

rename "s/.+_/ds/g" files

.+ represents everything up to (in this context) the last underscore (_) character (so this works with multiple underscores, as mentioned in your first example). This requires that there be at least one character before the underscore; if you might have file names like, use .* instead. So this three-character string (.+_ or .*_) will match everything up to and including the last underscore in the filename. s/old/new/ means substitute the new string (ds) for the old string. The g means global and might not be necessary in this case.

or, using the cross-platform renamer:

$ renamer --regex --find '.+_' --replace 'ds' *

Leave a Reply

Your email address will not be published. Required fields are marked *