« Trip to North Cyprus | Home | Web 2.0 parody »

How to rename files with bash

By Michael Stepanov | August 22, 2006

Recently I uploaded a few photos to the my site and I did a mistake in the name: Kirenia intead of Kyrenia. There were ten files but I didn’t want to change their names one by one. Thanks to bash. It offers an easy and elegant way to fix this problem:
for FILE in `find|grep Kirenia`; do TARGET=$(echo $FILE | sed -e "s/Ki/Ky/"); mv $FILE $TARGET; done;
P. S. I alwas forget about structure bash operator for:
for VARIABLE in LIST; do SOME_OPERATIONS ...; done

Topics: , , , |

 RSS feed for comments on this post.

2 Responses to “How to rename files with bash”

  1. Leonid Mamchenkov Says:
    August 22nd, 2006 at 3:17 pm

    There are even more elegant ways of doing this.

    First, find command provides the -exec switch, which allows you to execute a command (or a list of commands) on each found match.

    Second, there is a rename command, which would make your life much easier in this particular case.

    Cheers! :)

  2. Michael Stepanov Says:
    August 23rd, 2006 at 9:29 am

    Thanks, there is a more than one way to do it :)

Comments