Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

UNIX :renaming the files present in the directory

Shell Programming and Scripting


Closed    
 
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: 25 January 2016, 10:42 PM EST
Posts: 82
Thanks: 24
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 05: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: 23 November 2016, 10:12 AM EST
Posts: 199
Thanks: 3
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: 10 November 2016, 10:40 AM EST
Location: France
Posts: 2,948
Thanks: 82
Thanked 630 Times in 601 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 online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 4 December 2016, 9:09 AM EST
Location: Aachen, Germany
Posts: 9,900
Thanks: 199
Thanked 2,942 Times in 2,747 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: 10 November 2016, 10:40 AM EST
Location: France
Posts: 2,948
Thanks: 82
Thanked 630 Times in 601 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

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 04:39 AM
what is the use of each login related files present in users home directory chidori Solaris 6 06-22-2011 11:52 AM
Not able to edit files present in mounted directory subbaraju Solaris 2 06-03-2010 06:32 PM
renaming files in the directory c_d UNIX for Dummies Questions & Answers 4 05-11-2009 01:22 AM
Renaming files as per directory dotancohen Shell Programming and Scripting 2 03-22-2008 07:25 PM



All times are GMT -4. The time now is 10:12 AM.