The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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 !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
doing cat recursively on all files in a directory tulip Shell Programming and Scripting 11 05-21-2009 05:24 PM
recursively renaming files catgovind UNIX for Dummies Questions & Answers 2 08-20-2008 02:50 AM
rename the file in batch reldb UNIX for Advanced & Expert Users 7 01-14-2008 06:43 PM
rename in batch kathy18 Shell Programming and Scripting 3 03-09-2005 09:47 AM
List Files Recursively roberthawke UNIX for Dummies Questions & Answers 9 06-24-2002 06:19 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-26-2009
..Chris.. ..Chris.. is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 4
Exclamation Batch rename recursively

I would like to replace multiple underscores with hyphens but I have 26,000 files to rename. They are all in one file structure and multiple sub-directories.

It would be much simpler if I had a script to do it. Here are some samples of the file names:

Example 1
books/horrors/small/books_horrors_small_0000000386.jpg

Rename to

books/horrors/small/books-horrors-small-0000000386.jpg

Example 2
books/comedy/small/books_comedy_small_0000001687.jpg

Rename to

books/comedy/small/books-comedy-small-0000001687.jpg

There are 26,000 files that look similer to that and are all in different directories.

The only thing I need to do is replace all the underscores _ with hyphens -
  #2 (permalink)  
Old 06-26-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 375
Did you try anything? Atleast you should.
the below code will work but you try to write the for loop for this, atleast.
Code:
 sed 's/_/-/g'
  #3 (permalink)  
Old 06-26-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
Code:
awk 'BEGIN{
    q="\047"
    path="/path/to/search"
    cmd="find "path" -type f -name \"*_*.jpg\""
    while(( cmd|getline f )>0){
        m=split(f,file,"/")
        gsub(/_/,"-",file[m])
        newfilename = join(file,1,m,"/")            
        mv = "mv "q f q" "q newfilename q
        print mv
        #system(mv) #uncomment to use                     
        close(mv)
    }
    close(cmd)
}
function join(array, start, end, sep,    result, i)
{
         if (sep == "") sep = " "
         else if (sep == SUBSEP) sep = ""
         result = array[start]
         for (i = start + 1; i <= end; i++)
             result = result sep array[i]
         return result
}'

Last edited by ghostdog74; 06-26-2009 at 02:17 AM..
  #4 (permalink)  
Old 07-01-2009
..Chris.. ..Chris.. is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 4
Batch File Rename Recursively - Brilliant, Now How To on the Directory Names?

ghostdog74's solution below is BRILLIANT and worked well.

Follow up question:

How to do the exact same thing, but only on the file folders in that same file folder tree (recursively)?

DougABC123


Quote:
Originally Posted by ghostdog74 View Post
Code:
awk 'BEGIN{
    q="\047"
    path="/path/to/search"
    cmd="find "path" -type f -name \"*_*.jpg\""
    while(( cmd|getline f )>0){
        m=split(f,file,"/")
        gsub(/_/,"-",file[m])
        newfilename = join(file,1,m,"/")            
        mv = "mv "q f q" "q newfilename q
        print mv
        #system(mv) #uncomment to use                     
        close(mv)
    }
    close(cmd)
}
function join(array, start, end, sep,    result, i)
{
         if (sep == "") sep = " "
         else if (sep == SUBSEP) sep = ""
         result = array[start]
         for (i = start + 1; i <= end; i++)
             result = result sep array[i]
         return result
}'
Sponsored Links
Reply

Bookmarks

Tags
batch rename replace _ -

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 01:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0