Saturday, June 16, 2012

Shell Script to Count number of files in a directory




Q. How do I count number of files in a directory using Bash Script

Ans:

#!/bin/bash
echo "enter the complete path of your directory"
read n
if [ -z $n ]; then
#print number of files in current directory
ls -l | grep "^-" | wc -l
fi
#first check whether directory exist or not
test -d "$n" && echo -e "number of files in $n is `ls -l $n | grep "^-" | wc -l`\n" || echo -e "not a directory\n"


0 comments:

Post a Comment