Note. Edited to take care of a new line in / dir file names.
It is better not to analyze the output of the ls .
To count files (no directories):
find "$DIR" -maxdepth 1 -type f -exec echo -n . \; | wc -c
To count dirs:
find "$DIR" -maxdepth 1 -type d -exec echo -n . \; | wc -c
Your complete script:
#!/bin/bash echo 'Enter Dir names' read dirs for DIR in "$dirs"; do numFiles=$(find "$DIR" -maxdepth 1 -type f -exec echo -n . \; | wc -c) numDirs=$(find "$DIR" -maxdepth 1 -type d -exec echo -n . \; | wc -c) echo "Directory $DIR contains $numFiles files and $numDirs directories" done
source share