-2

Files

  events-number1.10a.pdf    

Result

 events-number1.10a.docx.pdf  

Ideal

 events-number1.10a.pdf   
 events-number1.10a.docx.pdf       
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
Dmomo
  • 83
  • 1
  • 1
  • 12

2 Answers2

0

You can try this simple bash script

#!/bin/bash
for file in  *.pdf
do
    new_file=$(echo "$file"  | sed -r 's/(.*)(\.pdf)/\1.docx\2/')
    mv $file $new_file
done

Output:

events-number1.index10a.docx.pdf 
events-number1.index10b.docx.pdf 
events-number1.index10c.docx.pdf 
events-number2.index10a.docx.pdf 
events-number2.index10b.docx.pdf 
events-number2.index10c.docx.pdf 

If you want copy the file using cp command instead of mv command

cp $file $new_file

So your existing files won't change.

Explanation :
Passing all the log file to for loop ,then split the file name to your expected result for using sed command and stored in one variable . Then mv the old file to new file that mean your expected file .

Kalanidhi
  • 4,902
  • 27
  • 42
  • Wow, while i am still watching tutorial. possible answer is ready! – Dmomo Nov 07 '14 at 09:47
  • Can you explian Wow, while i am still watching tutorial. possible answer is ready! Can you explain a little bit ? I don't just want the answer and it is not the ideal result, which means I need to works on that with your explanation. By the way, It doesn't work on Mac – Dmomo Nov 07 '14 at 09:55
  • Both two inner lines can be joined in one by using bash parameter expansion techniques: `mv "${file}" "${file#log}main.gmt.log"` – Jdamian Nov 07 '14 at 09:57
  • @user3096996,Updated my answer . – Kalanidhi Nov 07 '14 at 10:09
  • @Jdamian,Your suggestion is wrong , "${file#log}main.gmt.log" this output add the `main.gmt.log` in end of the file like `events-number2.index10a.log` but the result is `events-number2.index10a.main.gmt.log` – Kalanidhi Nov 07 '14 at 10:27
0

A simple rename command will do the job.

rename 's/(?=\.pdf$)/.docx/' *.pdf
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274