In order to just get the count of the number of directories (folders) at a location you would need to use multiple commands in conjunction.
Commands to be used to display the list of files/directories
- ls -l
- grep '^d.*'
- wc -l
If you use the ls command it will just display the list of files and directories at the given location. The trick here is used ls with option -l to display the list in long form.
Example:ls -l
total 0
drwxr-xr-x 7 code2care staff 224 May 13 19:46 AndroidStudioProjects
drwx------@ 3 code2care staff 96 Feb 13 16:04 Applications
drwxr-xr-x 4 code2care staff 128 Jan 17 16:16 ClassRoom
drwx------@ 9 code2care staff 288 Jun 26 12:57 Desktop
drwx------@ 13 code2care staff 416 Jun 5 17:11 Documents
drwx------+ 36 code2care staff 1152 Jun 26 12:56 Downloads
drwxr-xr-x 9 code2care staff 288 Apr 24 17:11 IdeaProjects
drwx------@ 76 code2care staff 2432 Apr 13 16:39 Library
drwx------ 6 code2care staff 192 Mar 20 16:54 Movies
drwx------+ 7 code2care staff 224 Mar 13 22:42 Music
drwx------+ 4 code2care staff 128 Jan 2 23:28 Pictures
drwxr-xr-x+ 4 code2care staff 128 Jan 2 23:03 Public
drwxr-xr-x 6 code2care staff 192 Jun 5 15:03 PycharmProjects
drwxr-xr-x 4 code2care staff 128 May 15 12:22 eclipse
drwxr-xr-x 4 code2care staff 128 May 15 13:07 eclipse-workspace
drwx------ 11 code2care staff 352 Apr 3 14:57 iCloud Drive (Archive)
drwxr-xr-x 2 code2care staff 64 Jun 5 17:20 myDir
-rw-r--r-- 1 code2care staff 0 Jun 5 17:19 sample.txt

As you can see the all the directories is denoted with a d and file has a - in instead. So you can use a command with grep to only filter those lines that starts with letter d using a regex '^d.*'
code2care@mac ~ % ls -l | grep '^d.*'
drwxr-xr-x 7 code2care staff 224 May 13 19:46 AndroidStudioProjects
drwx------@ 3 code2care staff 96 Feb 13 16:04 Applications
drwxr-xr-x 4 code2care staff 128 Jan 17 16:16 ClassRoom
drwx------@ 10 code2care staff 320 Jun 26 17:30 Desktop
drwx------@ 13 code2care staff 416 Jun 5 17:11 Documents
drwx------+ 36 code2care staff 1152 Jun 26 12:56 Downloads
drwxr-xr-x 9 code2care staff 288 Apr 24 17:11 IdeaProjects
drwx------@ 76 code2care staff 2432 Apr 13 16:39 Library
drwx------ 6 code2care staff 192 Mar 20 16:54 Movies
drwx------+ 7 code2care staff 224 Mar 13 22:42 Music
drwx------+ 4 code2care staff 128 Jan 2 23:28 Pictures
drwxr-xr-x+ 4 code2care staff 128 Jan 2 23:03 Public
drwxr-xr-x 6 code2care staff 192 Jun 5 15:03 PycharmProjects
drwxr-xr-x 4 code2care staff 128 May 15 12:22 eclipse
drwxr-xr-x 4 code2care staff 128 May 15 13:07 eclipse-workspace
drwx------ 11 code2care staff 352 Apr 3 14:57 iCloud Drive (Archive)
drwxr-xr-x 2 code2care staff 64 Jun 5 17:20 myDir
As you can see in the above, now ls along with grep shows only directories.
Now we need to do one more thing, make use of wc command to get the word count.
code2care@mac ~ % ls -l | grep '^d.*' | wc -l
17
Output:

✌️These commands would work on macOS, Linux and Unix Operating systems and Bash for Windows.
Have Questions? Post them here!
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!