LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash: make rename script traverse directories (http://www.linuxquestions.org/questions/programming-9/bash-make-rename-script-traverse-directories-499744/)

morrolan 11-08-2006 09:19 AM

bash: make rename script traverse directories
 
Hi, I have written a simple script to rename a load of files from *.pdf to *_.pdf.

The problem I have is that it can't traverse directories, as I have several hundred directories and I don't fancy running the script manually in each one. I have 3 levels of directories that I need it to look in - basically it just needs to do so recursively.

Here is the script:

Quote:

#!/bin/bash

for i in *.pdf
do
j=`echo $i | sed 's/.pdf/_.pdf/'`
mv $i $j
done
It works fine when run from within the folder containing some pdf's, but I can't run it from 1 level up, i.e. encompassing a couple of hundred folders each containing 10's of pdf's.

Any help would be greatly appreciated.


Morrolan

bigearsbilly 11-08-2006 10:51 AM

try
Code:

find . -name '*.pdf' | while read f
do
  echo mv $f ${f%.pdf}_.pdf
done

using echo for safety.

crabboy 11-08-2006 10:52 AM

try this:
Code:

for i in `find . -name '*.pdf'`


All times are GMT -5. The time now is 05:07 PM.