One thing I liked about Linux is it’s hidden simplicity. When in a hurry, I usually go for the first working solution of a given problem. If the same problem appears often, I can get used to the first solution so much that I don’t even think about simplifying it.
A good example of this scenario is recursive renaming of files. It doesn’t happen that often though, but when it does, I am usually in a hurry and use the same old solution that I came across years ago. Here is how I am used to do it:
[me@here dir]$ #Recursively rename some to other [me@here dir]$ for FILE in `find . -name somefile` [me@here dir]> do [me@here dir]> NEW=`echo $FILE | sed -e 's/some/other/'` [me@here dir]> mv "$FILE" "$NEW" [me@here dir]> done
Note that this is more of the base solution. Sometimes I have more parameters to
find. Like when I don’t use
sed at all and just use the
-exec mv syntax. Sometimes I go as far as write a Perl script.
Obviously, this solution is very ineffecient and overdesigned. But it always worked for me.
Today I had to recursively rename a bunch of files and for once I wasn’t in a hurry. While I was slowly enterting in the characters and made a typing mistake, it occured to me that I am overdoing something. So I stopped and made a miniresearch. I spent much less than two minutes on finding a simplier and much nicer way of doing the same.
Apparently, there is a standard utility
rename. It can change the name of the file fully or partially. It needs two parameters – what to change and to what to change it – and a list of file to work with. Here is how it can be used to solve the problem above:
[me@here dir]$ # Recursively rename some to other [me@here dir]$ rename some other `find . -name some`
Now that’s the beauty, isn’t it? It makes me think of all those characters I lost over the years…
: edited heavily to remove some nonsense that I typed for no good reason.