Shell script recursive function - traverse each directory to operate

  

. modify.func //import each function

modify .suf1 .suf2 //call function

put the current directory All file names ending with .suf1 are modified to end with .suf2, such as: modify .pc .ec

Recursively implements the subdirectories of the subdirectories. . . .

#!/bin/bash

#modify.func

doit() //Process non-directory files in the current directory, ignoring directory files {oldname=`ls |  Grep "$1$"`for name in $oldnamedoif [ -d "$name" ]then :else#prename=`echo $name |  Awk -F\\. '{print $1}'` #old method has bugsprename=${name%.*} #new methodecho -e "$PWD/$name\\t\\t$newname"mv $name $newnamecount= `expr ${count} + 1`fidone

return 0}

do_recursive() //Starting from the current directory, recursively processing each directory {doit $1 $2

for Filename in `ls`doif [ -d "$filename" ]thencd $filenamedo_recursive $1 $2cd ..fidone

return 0}

modify() //Process the current directory and report As a result, this is equivalent to the main function, you can also directly call do_recursive{PARAMS=2if [ $# -ne $PARAMS ]thenecho "usage: mv_to .suf1 .suf2"return 1ficount=0do_recursive $1 $2echo "complete! $count files Have been modified."

return 0}

Copyright © Windows knowledge All Rights Reserved