Mar
14
Posted on 14-03-2008
Filed Under (Shell scripting) by Arun Bagul

Introduction-

The UNIX/Linux Administrator or programmers have difficulties to solve this kind of quiries. Many times we need to rename perticular files within the given directories for example
we have to rename all the text file to conf file ie *.txt file to *.conf file….

We can use Pattern matching with String Operators in Shell scripting —

root@arunbagul:/home/arun/templates# touch main.txt data.txt readme.txt sample.txt demo.txt
root@arunbagul:/home/arun/templates# ls
data.txt demo.txt main.txt readme.txt sample.txt
root@arunbagul:/home/arun/templates#
Suppose you have thousands of *.txt files in given directory –

It is very difficult to rename this files to some other extension like *.conf
Take the above case and we will try to rename *.txt file by using shell script

root@arunbagul:/home/arun# ls -l /home/arun/templates/
total 0
-rw-r–r– 1 root root 0 2008-03-13 16:29 apache.conf
-rw-r–r– 1 root root 0 2008-03-14 02:50 data.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 demo.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 main.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 readme.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 sample.txt
root@arunbagul:/home/arun#

** In above “/home/arun/templates/” directory there are few *.txt files and
we will rename this files as *.conf files. There is apache.conf file, which will not rename.

root@arunbagul:/home/arun# ll /home/arun/templates/
total 0
-rw-r–r– 1 root root 0 2008-03-13 16:29 apache.conf
-rw-r–r– 1 root root 0 2008-03-14 02:50 data.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 demo.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 main.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 readme.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 sample.txt
root@arunbagul:/home/arun#

Now run our command as given below..

root@arunbagul:/home/arun# ./rename.sh /home/arun/templates txt conf
Rename all the *.txt files as .conf files…
Directory is = /home/arun/templates
renaminng /home/arun/templates/main.txt to /home/arun/templates/main.conf
renaminng /home/arun/templates/apache.txt to /home/arun/templates/apache.conf
renaminng /home/arun/templates/sample.txt to /home/arun/templates/sample.conf
renaminng /home/arun/templates/demo.txt to /home/arun/templates/demo.conf
renaminng /home/arun/templates/data.txt to /home/arun/templates/data.conf
renaminng /home/arun/templates/readme.txt to /home/arun/templates/readme.conf
root@arunbagul:/home/arun#

root@arunbagul:/home/arun# ls -l /home/arun/templates/
total 0
-rw-r–r– 1 root root 0 2008-03-13 16:29 apache.conf
-rw-r–r– 1 root root 0 2008-03-14 02:50 data.conf
-rw-r–r– 1 root root 0 2008-03-14 02:50 demo.conf
-rw-r–r– 1 root root 0 2008-03-14 02:50 main.conf
-rw-r–r– 1 root root 0 2008-03-14 02:50 readme.conf
-rw-r–r– 1 root root 0 2008-03-14 02:50 sample.conf
root@arunbagul:/home/arun#

Supose After some time we want to revert back!! ie want to rename *.conf file as *.
Run the above command and change the extension on command line as shown below

root@arunbagul:/home/arun# ./rename.sh /home/arun/templates/ conf txt
Rename all the *.conf files as .txt files…
Directory is = /home/arun/templates
renaminng /home/arun/templates/demo.conf to /home/arun/templates/demo.txt
renaminng /home/arun/templates/apache.conf to /home/arun/templates/apache.txt
renaminng /home/arun/templates/readme.conf to /home/arun/templates/readme.txt
renaminng /home/arun/templates/data.conf to /home/arun/templates/data.txt
renaminng /home/arun/templates/main.conf to /home/arun/templates/main.txt
renaminng /home/arun/templates/sample.conf to /home/arun/templates/sample.txt
root@arunbagul:/home/arun#

root@arunbagul:/home/arun# ll /home/arun/templates/
total 0
-rw-r–r– 1 root root 0 2008-03-13 16:29 apache.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 data.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 demo.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 main.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 readme.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 sample.txt
root@arunbagul:/home/arun#

Want to see the code ?

root@arunbagul:/home/arun# cat /home/arun/rename.sh
#! /bin/bash

path=$1
old_extension=$2
new_extension=$3
echo “Rename all the *.$old_extension files as .$new_extension files…”

#remove the trailing ‘/’ from end of the path
path=”${path%/}”
echo “Directory is = $path”

#Now, move/rename the files with given condition
#use ‘find’ command to find file path with given old extensions
for file_name in $(find $path -type f -name “*.${old_extension}”)
do
echo “renaminng $file_name to ${file_name%.${old_extension}}.${new_extension}”
mv -f $file_name ${file_name%.${old_extension}}.${new_extension}
done

#done
root@arunbagul:/home/arun#

NOTE :- Remember that this script is using ‘find’ command to find the files with given extensions
before renaming the files.. As ‘find’ command has no options to limit searching of files non recursively..
But still we can achieve non recursive renaming of files with same pattern matching and
string operation funcationality of shell script… please visit again!!

Thank you,

Arun Bagul

Similar Posts:

Be Sociable, Share!
(0) Comments    Read More   
Post a Comment
Name:
Email:
Website:
Comments:


www.flickr.com
arunbagul's photos More of arunbagul's photos
Get Adobe Flash player
-->