Renaming mutiple files with hyphens in name | 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.

Renaming mutiple files with hyphens in name

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-07-2011
akenne3 akenne3 is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 7 March 2011, 2:26 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Renaming mutiple files with hyphens in name

I have searched throught a host of threads to figure out how to rename mutiple files at once using a script.

I need to convert 200+ files from:

fKITLS_120605-0002-00001-000001.hdr to eStroop_001.hdr
fKITLS_120605-0002-00002-000002.hdr to eStroop_002.hdr
and so forth....

What is throwing me off is the hyphens (-) in the name. Any help would be greatly appreciated.
Thanks
Sponsored Links
    #2  
Old 03-07-2011
mvijayv mvijayv is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 25 March 2014, 6:02 AM EDT
Posts: 24
Thanks: 1
Thanked 0 Times in 0 Posts
cd to the directory that holds the files


Code:
rm mvcmds.sh
for filename in `ls -1|grep ^fK`
do
NewStr="eStroop_"
OldStr=`echo $filename|awk -F"." '{print substr($1,length($1) -2, length($1))}'`
NewName=""$NewStr""$OldStr"".hdr""
echo "mv $filename $NewName" >> mvcmds.sh
done

If you like the mv commands that are generated in the file mvcmds.sh, run it.
Hope this helps
Sponsored Links
    #3  
Old 03-07-2011
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 973 Times in 789 Posts
Assuming the dash-delimited field before the dot is always 6 characters wide:

Code:
for f in *.hdr; do
    mv "$f" eStroop_"${f##*-???}"
done

Regards,
Alister
    #4  
Old 03-07-2011
akenne3 akenne3 is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 7 March 2011, 2:26 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Command works lovely.
Thanks again for the help.
Sponsored Links
    #5  
Old 03-07-2011
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 973 Times in 789 Posts
Quote:
Originally Posted by mvijayv View Post

Code:
for filename in `ls -1|grep ^fK`

That will work with the filenames given in this thread, but in general it's a very poor practice.

Any filename containing an IFS character (by default a space, tab, or newline) would be broken into pieces after the command substitution, with each piece served up individually to a different loop iteration.

The -1 flag to ls is pointless when piping to another process. ls knows when it's not writing to a tty and will always output one file per line (although if a filename itself contains a newline, it's not possible to know where it ends).

Since pathname expansion happens after field splitting (so that there's no need to worry about what the filename may contain) and since there's no need for multiple fork-execs to create the ls-grep pipeline, a safe and more efficient alternative to your approach would be:


Code:
for filename in fK*

Regards,
Alister

P.S. And, yes, I agree. Anyone using newlines and tabs in filenames deserves what they get.
The Following User Says Thank You to alister For This Useful Post:
mvijayv (03-07-2011)
Sponsored Links
    #6  
Old 03-07-2011
mvijayv mvijayv is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 25 March 2014, 6:02 AM EDT
Posts: 24
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks Alister .. dont know what started me off with that approach a long time ago .. but never too late to get over a bad habit ... Good info, Thanks!
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
copy mutilple files to mutiple folders M474746 UNIX for Dummies Questions & Answers 4 01-10-2011 11:20 AM
Mutiple For loops - moving files to another directory tekster757 Shell Programming and Scripting 9 06-21-2007 08:42 AM
Sending mutiple files thru email to lotus notes lapisguy Shell Programming and Scripting 4 04-16-2007 06:28 PM
removing mutiple files run_time_error Shell Programming and Scripting 9 05-27-2005 07:40 PM
Report with mutiple files. Cameron Shell Programming and Scripting 4 01-28-2002 07:44 PM



All times are GMT -4. The time now is 07:47 PM.