Shell Script To recursively display (list) directories

This is a shell script to recursively display only the directories

#!/bin/sh
# Shell script to find out all the directories under a directory and
#its subdirectories. This also takes into consideration those files
#or directories which have spaces or newlines in their names

DIR="."

function list_files()
{
    if !(test -d "$1")
    then echo $1; return;
    fi

    cd "$1"
    echo; echo `pwd`:; #Display Directory name
for i in *
do
  if test -d "$i" #if dictionary
  then
     echo "$i"; #Display Directory name
  fi
done

for i in *
do
   if test -d "$i" #if dictionary
   then
      list_files "$i" #recursively list files
      cd ..
  fi
done

}

if [ $# -eq 0 ]
then list_files .
exit 0
fi

for i in $*
do
    DIR="$1"
    list_files "$DIR"
    shift 1 #To read next directory/file name
done
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s