November 27, 2016

What command would you use to count the subdirectories of the directory?

Answer to the question

Here is a fairly simple method:

tree -d -L 1

The -d only lists directories, and the -L 1 only lists the directories in "one level" (the current one). 

 

Like  (0 likes)

Answer to the question

Hi,

 

You can do something like the following to count the number of subdirectories under the current directory:

ls -la | grep '^d' | tail -n +3 | wc -l

If it's a different pathname than the current directory, add the pathname after the ls -la part, ie:

ls -la /pathname | grep '^d' | tail -n +3 | wc -l

Explaination:

ls -la - show all files including hidden pathnames

grep '^d' - show only directory entries (those beginning with a 'd')

tail -n +3 - show lines 3 and above, skipping the first two entries, which are . (the current directory) and .. (the parrent directory), which always appear

wc -l - count the number of lines shown in the output

 

I hope that helps.

 

Cheers,

SB

Like  (1 like)
Click Here!