Linux: regex batch file renaming
Context
Very often I find myself in the need to rename a batch of files, usually to remove, replace or to add content on a certain part of that filenames. Rewriting the contents of the files one by one, is the kind of nonesense one is supposed to get rid of when using a computer. It is, however, hard to find some free tool to do this, and often these tools are paid or have nonsense limitations.
I wrote this guide mostly as a code snippet for myself to come back to.
Use case
With this method you can rename files that match a description, match the sections that compose the filename and selectively remove, replace or append to each one of these sections. I also give some insight on how to make this task a bit more intuitive. One example is removing a section of the filename that makes the names unnecessarily long. Other possible use, is to homogenize certain text sections across all the files.
How to use
Combining Linux shell's find and rename commands, it is possible to use a regular expression to rename the files intelligently.
I usually create a regular expression that separates the name into sections of interest. Regular expressions can be a bit daunting at the beginning, but thanks to online tools such as regexr.com, you can try out how the expression works before touching the filenames. For security against your own mistakes, I advice you to do these changes only in folders that do not contain other folders, and keep a copy of the files without renaming safe somewhere else.
rename 's/(regex pattern)/(replace pattern)/' *
One important advice, if you don't want to mess up your files, is to use the -n flag before actually doing the renaming. The -n flag will make the command tell you the changes, but not actually change anything. In that way you can make sure that the expression works like you expected. I still recommend doing a backup anyway, just in case.
Source
I don't know my way around combining commands in Linux very well, but an answer to this stack overflow question offered the perfect solution: https://askubuntu.com/questions/283145/pattern-based-batch-file-rename-in-terminal. Later I discovered that I can have the same results I needed by just using rename command alone.
Example
I downloaded this sound pack from pjcohen user in freesound.org. Each file came with a preceding id number, different on each sample. I needed to remove this number.
I wrote the regular expression that separates the two areas of interest in the filename. The pattern "(\d{6}__)
" captures six digits followed by two underscores, in group 1 (a parenthesis creates a capture group in regex). Then "(.+)
" captures all the remaining characters, in capture group 2. I know my way around regex, but if you need to try before making changes, use an online tool to simulate the replacements. Simply do an ls
command, to get the list and use it as your test example text in there. Check this interactive example, which is more intuitive.
rename 's/(\d{6}__)(.+)/$2/' *.wav
Observe, too, that if I wanted, I could have re-ordered the sections of interest by simply changing the "replace" section of that regular expression with $2$1
.