January 08, 2007

Renaming files in a jiffy

Want to rename a bunch of files in a directory? If you are using a *nix operating system, it can be done with a single command. A set of files I downloaded from a website had filenames of this pattern: sitename.com_someprefix_filename.txt. If at all I had been using a Windows box, that would have been a nightmare -- I could not do it without writing a Python program. Fortunately, I am using a Kubuntu Linux box; so I can rename all the files with a single command:
for i in *.txt
do
   mv $i "`echo $i | sed 's/TheWebSite.com_[^_][^_]*_//'`"
done 
If I put this in a shell script, I won't even have to type this long command again and again. Now I realise why Windows is not for power users (like me).

3 comments:

  1. You are coming to say something about the interesting topics which you felt, much useful to others..

    But I donna abt this scripting..

    Thanks for giving this link.. Because by this only, I can browse some other collection done by you.. all are amazingly.. esp, the MNC - Match works & the article "Indian wife Payal" are very nice.

    ReplyDelete
  2. Hi,

    Nice going with linux and sify
    Could u please send me a new link or email the files. The link on the site has expired.

    Are those the only two files needed ?

    Thanks,
    Amar

    ReplyDelete
  3. @Amar: I have added a new post with the information about the new version of that script:
    http://mankikannan.blogspot.com/2007/08/sify-broadband-client-for-linux.html.

    Feel free to ask me if you face any problems. Let me know your comments about the script!-)

    ReplyDelete