Issue
I have a repo with the following structure and I wouldlike to countnumber of subdirectories inside tier1, tier2 and tier3. I dont want to count subdirectories within the subdirectory. For examplee i have folders named a 1, 2, 3 inside tier1 and i wanted to see the count as 3. I dont want whats isnide those 1,2,3 folders.
Git clone actions should be avoided, as we do not need a local clone of the whole repo plus all history information. A simple fetch will be enough, is there are any leaner ways to retrieve the directory information ??
Am presently counting number of subdirectories by, entering each folder and the folloing command:
ls | wc -l
Solution
ls
at best gives you the number of non-hidden entries directly inside the directory. If you have among them a plain file, or an entry containing spaces, or an entry where the name strats with a period, or a directory entry which is a directory, but has itself subdirectories, your count will be wrong.
I would instead do a
shopt -s nullglob
for topdir in tier[1-3]
do
if [[ -d $topdir ]]
then
a=($(find "$topdir" -type d))
echo "Number of subdirectories below $topdir is ${#a[@]}"
fi
done
The purpose of setting nullglob
is only to avoid an error message if no tier
directory exists.
UPDATE: If you are not interested in subdirectories further down, you could do instead of the find
a
shopt -s dotglob
a=("$topdir"/*/)
The dotglob ensures that you are not missing hidden subdirectories, and the final slash in the glob-pattern ensures that the expansion happens only for entries which denote a directory.
Answered By - user1934428 Answer Checked By - Katrina (WPSolving Volunteer)