![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 - |
|
||||
|
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.. |
|
||||
|
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:
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| batch rename replace _ - |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|