July 09, 2009 Archives

2009-07-09 23:55:44

Recursive Rename

Referring to my last blog entry, I'm proud to present an enhanced version of Larry Wall's/Robin Baker's famous script (p)rename. The main difference between the original and recrename lies within the fact, that the latter recursively renames all files/folders in the directory tree, when invoked with a directory name as argument.
So, here's an example how it works:

#"." represents the current directory...(more arguments are allowed)
recrename -n 's#\s+#_#g ;y#A-Z#a-z#;s#[^a-z0-9_\-.]#_#g;s#_+#_#g' .
If everything looks ok, you may omit the -n for the changes to take effect.

Update:
Since two programs for two very similar operations are a waste of precious disc space, I joined rename and recrename. From now on, recrename works recursively if you explicitly add -r to your command line. Furthermore, I changed the standard behaviour, so that modifications as from now on only affect basenames, even if the filename argument is a complete path, i.e. recrename 's#\s+#_#' "/home/foo/bar baz/bla blub" changes "bla blub" to bla_blub but leaves "bar baz" untouched.



Posted by haui | Permanent Link | Categories: linux, perl