#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    How to count directories in Unix script?


    I am learning shell scripting and I created the script below to count directories but it puts the list in memory and I have too many directories so I run out of memory with the error "There is not enough memory available now." Does anybody has suggestions on how to do this somehow else? Maybe write the list of directories to a file? Help me please!

    Code:
    #!/bin/sh
    START=$1
    DIRS=$(find "$START" -type d)
    for d in $DIRS
    do
          CNT=$(print "$(ls -l $d | grep dr | wc -l)" | nawk '{gsub(/^[ ]*/,"",$0); gsub(/[ ]*$/,"",$0)
          if [ "$CNT" != 0 ]; then
             echo "$d dirctory has "$CNT" directories" >> dircount.log
          fi
    done
    Thanks,
    Zepogi
  2. #2
  3. Type Cast Exception
    Devshed Supreme Being (6500+ posts)

    Join Date
    Apr 2004
    Location
    OAKLAND CA | Adam's Point (Fairyland)
    Posts
    14,954
    Rep Power
    8617
    This is a duplicate of this thread please reply to that, thread closed.
    medialint.com

    “Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss

IMN logo majestic logo threadwatch logo seochat tools logo