UNIX :renaming the files present in the directory | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

UNIX :renaming the files present in the directory

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-07-2013
scriptscript scriptscript is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 30 January 2014, 11:03 AM EST
Posts: 74
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 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 03-07-2013
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 26 June 2014, 10:29 AM EDT
Location: France
Posts: 2,929
Thanks: 80
Thanked 624 Times in 596 Posts

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

    #4  
Old 03-07-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 19 September 2014, 4:45 PM EDT
Location: Aachen, Germany
Posts: 4,273
Thanks: 72
Thanked 1,031 Times in 977 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 03-07-2013
mr.zeratul's Avatar
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 03-07-2013
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 26 June 2014, 10:29 AM EDT
Location: France
Posts: 2,929
Thanks: 80
Thanked 624 Times in 596 Posts

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

Sponsored Links
    #7  
Old 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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 02:01 PM.