Showing posts with label shell script to count files and sub-directories. Show all posts
Showing posts with label shell script to count files and sub-directories. Show all posts

Saturday 6 April 2019

Count number of files and sub-directories in a given directory

Shell script to input a directory name and display the number of files and
sub directories present in the given directory.


Program
echo -n "Enter directory name: "
read dr
dircnt=`ls -l $dr|grep "^d"|wc -l`
filecnt=`ls -l $dr|grep "^-"|wc -l`
echo "No of directories = $dircnt"
echo "No of files = $filecnt"


OUTPUT
Safi $ sh dircnt.sh
Enter directory name: .
No of directories = 10
No of files = 98


Safi $ sh dircnt.sh
Enter directory name: sasha
No of directories = 0
No of files = 4