Quick Multi-File Rename in Linux

less than 1 minute read

Call it the little things, but I'm usually excited when I find a new tool that speeds things up.  I accidentally ran a set of experiments that produced multiple files that were named incorrectly.  That is, I wrote files as aq*-female-1_day-*_event*.csv but I was actually supposed to write the files as aq*-female-edt-*_event*.csv.  These files fit into a larger set of results with varying time lengths (edt, month, year, etc), so I needed to change dozens of files to fix my mistake.

Cue the head scratching.  Do I rename each individually?  Do I write a bash or PHP script to perform the renaming, perhaps taking time away from other things? No! I found out there's a tool for that built into the Linux command line environment: rename.

The rename command takes a Perl regular expression and a set of files and performs a rename on them based on the regular expression.  So, in this way, I could rename dozens of files with one command:

> rename 's/(.*)-1_day-(.*)/\1-edt-\2/' *.csv