
November 3rd, 2010, 09:46 AM
|
|
Registered User
|
|
Join Date: Nov 2010
Posts: 3
Time spent in forums: 51 m 50 sec
Reputation 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
|