I know that my following task could be accomplished using simpler 'find' comment, but I am trying to implement a solution using a recursive call. I am looking through a specific directory and trying to get max length of any filename in all the sub-directories. However, my recursion works only one level down, so it basically returns me the longest filename in a certain directory or in its' subdirectories.
#! /bin/bash
export maxlen=0
findmaxr()
{
if [ $# -eq 0 ] ; then
echo "Please pass arguments. Usage: findmax dir"
exit -1
fi
if [ ! -d "$1" ]; then
echo "No such directory exist."
exit -2
fi
for file in $(/bin/ls $1)
do
if [ -d "$file" ] ; then
findmaxr $file # Recursively call the method for subdirectories
else
cur=${#file}
if [ $maxlen -lt $cur ] ; then
maxlen=$cur
fi
fi
done
echo "The file with the longest name has [$maxlen] characters."
}
findmaxr `pwd`