16 June 2012

Shell Script to Count number of files in a directory

Leave a Comment

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"

Output


count no of files in a directory

Enjoy :)


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

0 comments:

Post a Comment