Renaming with wildcards | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Renaming with wildcards

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-09-2013
rnmuk rnmuk is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 9 July 2013, 5:20 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Renaming with wildcards

Hi, I'm new to Unix, but have a directory which has many files in it, well over 1000. The files are called :


Code:
M07GO.STOPE0001
M07GO.STOPE0002
M07GO.STOPE0003
M07GO.STOPE0004

etc...

I would like to rename them to the following :

Code:
M070001.bin
M070002.bin
M070003.bin
M070004.bin

etc....

so missing out the "GO.STOPE" part of the original name and adding ".bin" onto the end. Is there a single command I can type that will rename all the files, possibly by using the wildcard function?
Many thanks, Robert

Last edited by Scott; 07-09-2013 at 06:19 PM.. Reason: Please use code tags
Sponsored Links
    #2  
Old 07-09-2013
bartus11's Avatar
bartus11 bartus11 is online now Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 19 April 2014, 5:05 AM EDT
Posts: 3,679
Thanks: 6
Thanked 1,131 Times in 1,104 Posts
Try:
Code:
for i in *; do mv $i `echo $i | sed 's/GO.STOPE//;s/$/.bin/'; done

The Following User Says Thank You to bartus11 For This Useful Post:
gacanepa (07-10-2013)
Sponsored Links
    #3  
Old 07-10-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 19 April 2014, 5:02 AM EDT
Location: Aachen, Germany
Posts: 3,514
Thanks: 51
Thanked 819 Times in 782 Posts
In recent shells (bash, ksh) use parameter pattern deletion:
Code:
for X in M*; do mv $X ${X%GO*}${X#*PE}.bin; done

EDIT: corrected as proposed below by scrutinizer

Last edited by RudiC; 07-10-2013 at 04:53 PM..
    #4  
Old 07-10-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 19 April 2014, 5:12 AM EDT
Location: Amsterdam
Posts: 8,874
Thanks: 230
Thanked 2,141 Times in 1,922 Posts
The latter method is more efficient, and it should work in any POSIX compliant shell.

---
@RudiC: small typo, it should be mv "$X"
The Following User Says Thank You to Scrutinizer For This Useful Post:
RudiC (07-10-2013)
Sponsored Links
    #5  
Old 07-10-2013
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 4 April 2014, 4:57 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,359
Thanks: 8
Thanked 534 Times in 513 Posts
Since it is going to do a lot of mv, consider breaking it up:
Code:
$ ls *GO.STOPE* | sed 's/^\(.*\)GO\.STOPE\(.*\)$/mv & \1\2/' | sh

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
Wildcards cokedude UNIX for Advanced & Expert Users 1 10-24-2010 04:07 AM
wildcards NOT C3000 UNIX for Dummies Questions & Answers 5 11-21-2007 06:19 AM
ls with wildcards benu302000 UNIX for Dummies Questions & Answers 10 06-29-2005 01:53 PM
wildcards benu302000 UNIX for Dummies Questions & Answers 3 06-29-2005 12:10 PM
Wildcards in VI peter.herlihy UNIX for Dummies Questions & Answers 8 01-08-2002 03:27 AM



All times are GMT -4. The time now is 05:26 AM.