A shell script for Renaming large number of files in Linux

29 10 2011

Hi, today I’m going to Blog about a very simple shell script, that may help you guys a lot.NO big deal, its very simple one. It was very helpful to me, once I had to rename a large number of images of Kim Kardashian.  So then I realise it will very easy doing it with a Shell script rather than manually rename it.#!/bin/bash
# Author: Gihan De Silva @  gihansblog.com
# rename script
# rename.sh
for i in `ls *.jpg`
x=`expr $x + 1`
mv $i Kim_Kardashian$x.jpg

echo “rename done!”

Code Explanation!


This is how always a shell script starts.


This line clears all the things already in the terminal window.


This line define and declare a variable.

for i in `ls *.jpg`

This line starts a loop and, it will list down all the JPEG(jpg) type images.

x=`expr $x + 1`

This line increases the value of x by 1.

mv $i Kim_Kardashian$x.jpg

This line renames all the files according to a sequence like, Kim_Kardashian1.jpg , Kim_Kardashian2.jpg, Kim_Kardashian3.jpg  ……

And you can just make Kim_Kardashian$x.jpg like My_file_name_$x.jpg ,so that’s it.

And to run this shell script, put rename.sh shell script into your file folder (directory) .

Then open Terminal window (Ctrl+T).

Go to your file folder through the terminal.

Then type chmod 755 rename.sh to get the permission.

Then run it by typing ./rename.sh

Now you will see files have renamed :D.

You can DOWNLOAD the shell script from here!

Thank you

Gihan De Silva




4 responses

6 11 2011
Prestashop Templates

Online Article…

[…]very few websites that happen to be detailed below, from our point of view are undoubtedly well worth checking out[…]…

8 11 2011
Web design Philadelphia

Hi there.This valuable submit was really inviting, primarily since i have got was first attempting to find looking for grants this approach matter ultimate Saturday.

9 11 2011
Web design Philadelphia

“I would like to thank you for the time you have contributed in composing this blog post”

8 12 2011

Tanks… i made a simple improvement of your code. It’s there:
It’s GPL licensed, right?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: