I had a group of files that I'd like to consistently rename, the files are named things like
"System-Log-01-01-2009-NODATA.txt"
"Something-Log-01-01-2009-NODATA.txt"
And I wanted them as lowercase, yyyymmdd, .log extension
"system.20090101.log"
"something.20090101.log"
I used to write perl scripts to do this, until I discovered the rename command.
It accepts a perl regex to do the rename:
for this, I just typed two commands:
For some distros though,
rename
doesn't have this functionality (see its man page), and you may have to installperl-rename
orprename
.mmv is a standard linux utility to move/rename multiple files. It is available from the repos for most distributions. For your example above, you could do:
For more information, read this debaday article or the man page.
Since i don't have a rename command, i am relying on this:
rename
util is not very "standard". Each distro ships with a differentrename
tool. For instance, here on Gentoo,rename
is fromsys-apps/util-linux
package and does not support regex.Hamish Downer suggested
mmv
, it seems useful, specially for use inside scripts.On the other hand, for the general case, you might want renameutils. It has
qmv
andqcp
commands, which will open a text editor of your choice (my preference: Vim) and allow you to edit the destination filenames there. After saving and closing the editor,qmv
/qcp
will do all the renaming.Both
mmv
andqmv
are smart enough to rename files in correct order and also to detect circular renames, and will automatically make a temporary file if needed.To be fair:
gives this output:
But:
rename -n 's/(\w+)-\w+-(\d{2})-(\d{2})-(\d{4})-NODATA\.txt$/$1.$4$3$2\.log/' *.txt && rename 'y/A-Z/a-z/' System.20090101.log
gives the right output:
replacing {-n} switch with {-v}
I created a small bash script to do this:
I have stumbled upon a python command-line tool called regex-rename, to install it, just run:
To test your regex:
The actual renaming would be: