Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

UNIX :renaming the files present in the directory

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-07-2013
scriptscript scriptscript is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 16 November 2014, 9:00 AM EST
Posts: 75
Thanks: 21
Thanked 0 Times in 0 Posts
UNIX :renaming the files present in the directory

Hi all,

I am looking for a script which renames all the files from the present directory.

Eg.:
In unix directory contains the below files


Code:
linux001.txt
linux002.txt
linux003.txt
......
.......

Now the files should be renamed to

Code:
unix001.txt
unix002.txt
unix003.txt

Could anyone please help me on the above requirement.

Thanks in advance...

Regards,
J

Last edited by Scrutinizer; 03-07-2013 at 04:59 AM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 03-07-2013
busyboy busyboy is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 19 March 2014, 3:51 AM EDT
Posts: 191
Thanks: 2
Thanked 9 Times in 9 Posts
its a sample code..try with relevant names/paths. use the mv command instead.



Code:
for i in `seq -f 'linux%03.f.txt' 1 100`; do  echo $i; echo $i|awk '{ gsub(/^linux/,"unix",$0);print}'; done

---------- Post updated at 03:20 PM ---------- Previous update was at 03:18 PM ----------


Code:
for i in `seq -f 'linux%03.f.txt' 1 100`; do  mv $i  `echo ${i/linux/unix}`; done

Sponsored Links
    #3  
Old Unix and Linux 03-07-2013
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 22 July 2015, 12:40 PM EDT
Location: France
Posts: 2,933
Thanks: 81
Thanked 627 Times in 598 Posts

Code:
for i in linux*.txt
do
cp -p "$i" unix"${i#*x}" && rm "$i"
done

    #4  
Old Unix and Linux 03-07-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 2 August 2015, 12:44 PM EDT
Location: Aachen, Germany
Posts: 6,411
Thanks: 112
Thanked 1,770 Times in 1,656 Posts
busyboy is on the right track! Try
Code:
$ for i in linux*; do echo mv $i ${i/linux/unix}; done
mv linux001.txt unix001.txt
mv linux002.txt unix002.txt
mv linux003.txt unix003.txt

Remove echo if happy with results.
Sponsored Links
    #5  
Old Unix and Linux 03-07-2013
mr.zeratul's Unix or Linux Image
mr.zeratul mr.zeratul is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 7 March 2013, 11:00 PM EST
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
Hello everybody,

I'm still a newbie, but here's my attempt:

$ for i in * ; do mv $i uni${i##linu}; done
The Following User Says Thank You to mr.zeratul For This Useful Post:
rbatte1 (03-07-2013)
Sponsored Links
    #6  
Old Unix and Linux 03-07-2013
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 22 July 2015, 12:40 PM EDT
Location: France
Posts: 2,933
Thanks: 81
Thanked 627 Times in 598 Posts

Code:
# i=linux001.txt
# echo $i
linux001.txt
# echo ${i#*x}
001.txt
# echo unix${i#*x}
unix001.txt
#

Sponsored Links
    #7  
Old Unix and Linux 03-07-2013
Ravi Bhagat Ravi Bhagat is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 7 March 2013, 2:34 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
please run and let me know.

#! /bin/bash
list=(`ls`);
j=0;
len=${#list[*]};
while [ $j -lt $len ]
do
mv ${list[$j]} $(echo ${list[$j]} | sed 's/linux/unix/g');
j=`expr $j + 1`;
done
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Checking in a directory how many files are present and basing on that merge all the files srikanth_sagi Shell Programming and Scripting 10 01-02-2013 03:39 AM
what is the use of each login related files present in users home directory chidori Solaris 6 06-22-2011 10:52 AM
Not able to edit files present in mounted directory subbaraju Solaris 2 06-03-2010 05:32 PM
renaming files in the directory c_d UNIX for Dummies Questions & Answers 4 05-11-2009 12:22 AM
Renaming files as per directory dotancohen Shell Programming and Scripting 2 03-22-2008 06:25 PM



All times are GMT -4. The time now is 08:26 PM.