Batch rename recursively


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Batch rename recursively
# 1  
Old 06-26-2009
Error 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  
Old 06-26-2009
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  
Old 06-26-2009
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 03:17 AM..
# 4  
Old 07-01-2009
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
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
}'

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find and rename file recursively

Hi, I have a directory which contains multiple files with .txt extension, i want to rename all these file to .bak extension using find command, this is what i've tried, please help me to correct this : find /home/application/test -name '*.txt' -exec rename 's/txt/bak/' {} \; seems to... (8 Replies)
Discussion started by: mukulverma2408
8 Replies

2. Shell Programming and Scripting

Batch Script To Unzip and Rename File

Hello All, I need help in writing a batch script. I have 100 zip files in a folder. Each zip file has a unique name starting with XYZ_12345.zip Each zip file contains single csv file I would like to batch extract the files and also rename the extracted csv as per the original zip name... (6 Replies)
Discussion started by: rajlakshmi
6 Replies

3. Shell Programming and Scripting

Batch rename files that do not match pattern

Hi all, I do not have programming experience and mostly use one-liner or sometimes more to get the job done. I am having problem to batch rename the files that do not match a particular pattern. Sample file-names from directory: Meeting_Packages.pdf 13_textfile0 19_textfile0 ... (3 Replies)
Discussion started by: atulkakrana
3 Replies

4. Shell Programming and Scripting

Recursively rename directories

I have this directory tree under /apps/myapp/data: imageshack.us/photo/my-images/703/foldersc.png How to recursively rename ONLY directories with 5 digits (00000, 00100, 00200,..., 00007, 00107,...)? I want to add to their name two more zeros: Before: 00107 After: 0000107 Thanks in... (2 Replies)
Discussion started by: Susan_45
2 Replies

5. Shell Programming and Scripting

Recursively rename some files

Hello, I have one directory with 3 level sub-directories, and about houndard files under those directories. I need a shell script to rename all patern mateched directories and files. For example: the patern is AA in the directory or file name. Orignal directory:... (2 Replies)
Discussion started by: mail4mz
2 Replies

6. Shell Programming and Scripting

try to batch rename using sed (if this is best)

hi gooday I need some help with a rename I am attempting. I'd like to rename a bunch of files in a folder example list.dat.old to list_N.dat query.dat.old to query_N.dat note the two periods in (.dat.old) to become _N.dat I tried using sed like this ls *.dat.old | sed... (3 Replies)
Discussion started by: johnstrong
3 Replies

7. Shell Programming and Scripting

Batch rename files after internal search pattern

Hello all I am trying to do a script that would allow me to mass rename files after a grep search within them. They are XML files and the pattern I would like to append to the file name is easy to find (e.g. <filename>foo</filename>), but I can't for the life of me find out how to do it and... (2 Replies)
Discussion started by: aavv
2 Replies

8. Shell Programming and Scripting

Rename files recursively

hi I have files named 123_234_aaa.jpg 234_231_345.jpg and i wish to rename these files to aaa.jpg and 345.jpg. i.e inital number,_,next number should be removed form the file name. Please let me know how can i do it. (2 Replies)
Discussion started by: vasuarjula
2 Replies

9. UNIX for Advanced & Expert Users

rename the file in batch

In my dir there are files like a.xml b.xml abnc.xml 12.abc.xml 12.anc.sfoioi.xml I need to remove .xml from all the files Is there any direct way without using any for/do loop Right now i am using for file in * do ... (7 Replies)
Discussion started by: reldb
7 Replies

10. Shell Programming and Scripting

rename in batch

example test1 will have m1234567.12a I would like to rename in batch but I don't Please help me on this. cd /a1/a2/a3 test1=$(basename /a1/a2/a3/*.*) >> /tmp/t echo $test1 echo "Extracting 8 th position" >> /tmp/t2 awk '{print substr($1,8,1); }' $test1 >> /tmp/t3 echo "extraction ... (3 Replies)
Discussion started by: kathy18
3 Replies
Login or Register to Ask a Question