Replace characters in all file names in a particular 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.

Replace characters in all file names in a particular directory

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-16-2006
madhunk madhunk is offline
Registered User
 
Join Date: Nov 2005
Last Activity: 22 May 2013, 5:40 PM EDT
Posts: 95
Thanks: 1
Thanked 0 Times in 0 Posts
Replace characters in all file names in a particular directory

Hi,

I have searched the forum on how to mass replace the file names. We are doing the migration and I am trying to accomplish a task where I have to replace all UNIX scripts in a particular directory that start with bdw to fdm...

For example: bdw0110137.sh should be fdm0110137.sh

Keep the existing script bdw0110137.sh and the contents in it and also have another script fdm0110137.sh with the same contents. Just like copying it into another script but with 'fdm' in the beginning.

I tried using the cut command, and also tried to change the code I found in the forum.

I would appreciate if you could help me in this regard.

Thank You,
Madhu
Sponsored Links
    #2  
Old 02-16-2006
madhunk madhunk is offline
Registered User
 
Join Date: Nov 2005
Last Activity: 22 May 2013, 5:40 PM EDT
Posts: 95
Thanks: 1
Thanked 0 Times in 0 Posts
Kind of achieved by this

#!/bin/ksh
for e in *;
do mv "$e" "`echo $e | sed -e 's/\bdw/fdm/g'`";
done

But it changed the internal contents of the file too....

Is there a better way to achieve this?
Sponsored Links
    #3  
Old 02-16-2006
bigearsbilly bigearsbilly is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 19 November 2014, 2:19 AM EST
Location: Southern England
Posts: 247
Thanks: 0
Thanked 10 Times in 10 Posts
for f in bdw* ;do
echo mv $f fdm${f%bdw}
done
    #4  
Old 02-16-2006
RTM's Avatar
RTM RTM is offline Forum Advisor  
Registered User
 
Join Date: Apr 2002
Last Activity: 17 October 2014, 8:46 PM EDT
Location: On my motorcycle
Posts: 3,093
Thanks: 1
Thanked 32 Times in 9 Posts

Code:
#!/bin/csh
# Note - run in proper directory
#
ls -1 fdm* > /tmp/bdw.list
set filelist=`cat /tmp/bdw.list`
foreach x ($filelist)
        set newname=`echo $x|sed 's/fdm/bdw/g'`
        echo $newname
        cp $x $newname
end

Sponsored Links
    #5  
Old 02-16-2006
bigearsbilly bigearsbilly is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 19 November 2014, 2:19 AM EST
Location: Southern England
Posts: 247
Thanks: 0
Thanked 10 Times in 10 Posts
oops! slight error should be # not %

Code:
#!/bin/bash

for f in bdw* ;do
        echo mv $f fdm${f#bdw}
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
share a shell script which can replace weird characters in directory or file name begonia Shell Programming and Scripting 5 03-13-2012 09:23 PM
Grepping file names, comparing them to a directory of files, and moving them into a new directory sHockz Shell Programming and Scripting 1 02-02-2012 02:53 AM
Searching for file names in a directory while ignoring certain file names 2reperry Shell Programming and Scripting 2 12-13-2009 10:16 PM
removing first 7 characters of directory names euspilapteryx UNIX for Dummies Questions & Answers 4 10-22-2009 12:24 AM
Weird Ascii characters in file names yamsin789 Shell Programming and Scripting 2 10-07-2007 11:27 AM



All times are GMT -4. The time now is 10:49 PM.