Renaming all files in a folder | Unix Linux Forums | UNIX for Advanced & Expert Users

  Unix/Linux Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Renaming all files in a folder

UNIX for Advanced & Expert Users


Tags
rename files

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-28-2010
appualex appualex is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 8 April 2011, 12:59 AM EDT
Location: Bangalore, India
Posts: 13
Thanks: 17
Thanked 0 Times in 0 Posts
Renaming all files in a folder

Guys, i need to replace a particular pattern in lots of files in a folder with another pattern . Is there any command line script I can use to do the functionality.

Eg:
Code:
aalex > ls
green_1234_colour.max
red_1234_pen.max
yellow1234flower.max

Need to replace the 1234 in the file name with 9876 i.e,


Code:
aalex > ls
green_9876_colour.max
red_9876_pen.max
yellow9876flower.max

How can i do this??

Alex

Last edited by Yogesh Sawant; 07-06-2010 at 03:23 AM.. Reason: added code tags
Sponsored Links
    #2  
Old Unix and Linux 06-28-2010
pseudocoder's Unix or Linux Image
pseudocoder pseudocoder is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 17 February 2012, 6:42 AM EST
Location: /home/sea
Posts: 602
Thanks: 40
Thanked 83 Times in 81 Posts

Code:
$ for i in *1234*.max; do
mv -v "$i" "$(echo $i | sed 's/1234/9876/')"
done
green_1234_colour.max -> green_9876_colour.max
red_1234_pen.max -> red_9876_pen.max
yellow1234flower.max -> yellow9876flower.max
$

Sponsored Links
    #3  
Old Unix and Linux 07-06-2010
appualex appualex is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 8 April 2011, 12:59 AM EDT
Location: Bangalore, India
Posts: 13
Thanks: 17
Thanked 0 Times in 0 Posts
Thank u guys for the replay.
I think rename doesn't work for this situation as we need to look for a particular expression and replace it with a new pattern.
    #4  
Old Unix and Linux 07-06-2010
thegeek thegeek is offline
Read Only
 
Join Date: Apr 2009
Last Activity: 30 July 2012, 5:38 AM EDT
Location: /usr/bin/vim
Posts: 946
Thanks: 13
Thanked 38 Times in 36 Posts
Quote:
Originally Posted by appualex View Post
Thank u guys for the replay.
I think rename doesn't work for this situation as we need to look for a particular expression and replace it with a new pattern.

Why not ? rename is for renaming right ?! what expression you are thinking of ?


Code:
 
# ls
green_1234_colour.max  red_1234_pen.max  yellow1234flower.max
# rename 1234 9846 *
# ls
green_9846_colour.max  red_9846_pen.max  yellow9846flower.max

Sponsored Links
    #5  
Old Unix and Linux 07-07-2010
appualex appualex is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 8 April 2011, 12:59 AM EDT
Location: Bangalore, India
Posts: 13
Thanks: 17
Thanked 0 Times in 0 Posts
Sorry dude, i didint think like this..as this pattern comes in between...great.Unix or Linux Image.thanks
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find all text files in folder and then copy to a new folder cgkmal Shell Programming and Scripting 4 06-20-2009 02:12 PM
check how many files in folder or total files in folder durgaprasad Shell Programming and Scripting 3 06-16-2009 04:26 AM
script for Finding files in a folder and copying to another folder satish2712 Shell Programming and Scripting 5 09-11-2008 05:07 PM
Auto copy for files from folder to folder upon instant writing Bashar UNIX for Advanced & Expert Users 2 08-21-2008 03:44 PM
Getting error by renaming all the files in a folder sunitachoudhury Shell Programming and Scripting 4 04-10-2008 07:34 AM



All times are GMT -4. The time now is 03:27 AM.