Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have around 300 files named


I need to rename them all to


How can i do this?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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 _20131012.zip, 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.

share|improve this answer
To be sure that the regexp matches from the beginning I would rather put ^ to the beginning. Also OP wanted to retain the underscore. The g option at the end is not really needed here - it would match multiple instances in the file name if possible. So I think this command would perform better: rename "s/^.+_/ds_/" files –  pabouk Oct 12 '13 at 21:40
adding the g is just a standard habit from me and i agree that it's not needed in this case. –  noggerl Oct 12 '13 at 21:45

or, using the cross-platform renamer:

$ renamer --regex --find '.+_' --replace 'ds' *
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.