LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   working with grip (https://www.linuxquestions.org/questions/linux-newbie-8/working-with-grip-736059/)

Jason40k 06-27-2009 12:43 PM

working with grip
 
how would i modify this function to descend all subdirectories of the named directory recursively and to find the maximum length of any filename in that hierarchy.

$ function maxfn () {
> typeset -i max thisone
> if [ ! -d "$1" -o $# = 0 ]
> then
> echo "Usage: maxfn dirname"
> return 1
> fi
>
> max=0
> for fn in $(/bin/ls $1)
> do
> thisone=${#fn}
> if [ $thisone -gt $max ]
> then
> max=$thisone
> fi
> done
> echo "Longest filename is $max characters."
> }

Uncle_Theodore 06-27-2009 01:37 PM

Sometime ago I wrote a simple script to run over a directory tree recursively.
Here it is. I think you can adapt it to your problem easily.

Code:

#!/bin/bash

recursewalk(){
    for file in *
    do if [ -d $file ]
            then echo "Descending into $file"
            cd $file
            recursewalk
            cd ..
        fi
    done
}
recursewalk



All times are GMT -5. The time now is 05:06 AM.