How can number of subdirectories and files be determined from the output of using ls -ld command?

I realize that this command only lists the directories.

closed as unclear what you're asking by slm, jasonwryan, Anthon, Rahul Patil, iruvar Nov 17 '13 at 20:21

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Please provide a simple example of the input and output you're looking for. Do you want a count of only subdirectories and files at depth 1, or do you want to traverse the entire directory tree? Is it important that the solution use ls -ld for some reason? – dg99 Nov 17 '13 at 4:31
  • Please add some more details please. – slm Nov 17 '13 at 5:19
  • You can determine the number of subdirectories one level down from the second column of output (number of hard links): you take that number and subtract 2 to get the number of subdirectories. – Joseph R. Nov 17 '13 at 8:17
  • Yes it is important that the command is ls -ld. Apparently you can find the number of sub directories and files just from the output of using this command in a directory but I do not see how it is possible. – John Nov 17 '13 at 13:23
  • ls -ld isn't restricted to directories (this isn't what -d means). What are you trying to do? – Gilles Nov 17 '13 at 20:44

There are so many different possibilities...

If you want to count your files,

 ls -l | grep ^- | wc -l

same thing for directories,

 ls -l | grep ^d | wc -l

The sum of the two

 ls -l | wc -l

All subdirectories within a tree starting with current directory:

find . -type d -print | wc -l

same thing for all files

find . -type f -print | wc -l

or perhaps for links

find . -type l -print | wc -l

The rest by induction

Not the answer you're looking for? Browse other questions tagged or ask your own question.