16 June 2012

Shell Script to Count number of files in a directory

1 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...

1 comment:

  1. Appreciate your effort in sharing this detailed and easy-to-understand post. Check the Reaction Time Test profile for more reliable knowledge like this. Reaction speed testing is often used in sports psychology as well.

    ReplyDelete