All I am trying to do is get a count of direcrories on one leve of a tree. so Far I have this.

find /media/data/temp1  -type d -exec basename {} \;

which gives me this:

Greatest Hits
Sex Pistols
never mind the bollocks here's the sex pistols
the great rock and roll swindle
The Boomtown Rats
Doris Day
Greatest Hits

To rid me of all of the extra directories that are actually subDirs of the the "base" dir just under temp1 and to get rid of the dirertory that contains the directories I need to get a count of I do this:

find /media/data/temp1 -mindepth 1 -maxdepth 1    -type d -exec basename {} \; 

Which gets me this:

Sex Pistols
The Boomtown Rats
Doris Day

The Actual Directories on the level that I need to get a count of all Directories on this very same level. So I can use it to keep track of in a counter such as

let TotalDir--
echo "Dir left to do is "$TotalDir""

So I know there has to be a simple function that I can pipe that into in the syntax such as this to get what I need.

TotalDir=$( echo find /media/data/temp1 -mindepth 1 -maxdepth 1 -type d -exec basename {} \;| "the function name needed here" )

I just do not know what function call to use, or what I'd call them little programs within Linux that most have little knowleage that they are even there and others have loads of knowleadge on how to use them.


1 Answer 1


the function you are looking for is wc (word count) with the option -l to count lines (that is the letter l not the number one).

However, you don't need find for this task. A much easier way to do what you want is this:

TotalDir=$( ls -1d */ | wc -l )

note it is the number one after ls and the letter l after wc.

Edit: Summarizing the discussion in the comments below, here is a little more explanation of how this solution works:

  • -1 tells ls to use only one column which ensures that we really get one line per subdirectory
  • -d tells ls to only list directories and not their contents (just try ls */ with and without the -d to see the difference.
  • */ tells ls to list all directories below the current one. /path/to/ParentDirectory/*/ would tell ls to list all directories below /path/to/ParentDirectory/
  • finally, wc -l counts the lines of the output of the ls command which --with the parameters explained above-- is exactly the number of subdirectories.
  • 1d indecates one dir deep, and the */ is path? where */ then it would read TotalDIr=$( ls -1d /media/data/temp1/ | wc -l ) yes?
    – uxserx-bw
    Nov 29, 2015 at 2:48
  • @userx-bw, -1d is two separate switches, -1 indicates put each listed item on a separate line and -d indicates list directories instead of their contents.
    – gogoud
    Nov 29, 2015 at 7:22
  • @userx-bw, if you want to count subdirectorries in /media/data/temp1/ then the command would look like this: TotalDir=$( ls -1d /media/data/temp1/*/ | wc -l )
    – Thawn
    Nov 29, 2015 at 18:27
  • ahhhh so it only goes in One Deep and the -1d insures that it tells ls to only go one deep as well --yep i see this now -- thanks ! -
    – uxserx-bw
    Nov 29, 2015 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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