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!
- How to create a New Project in Visual Studio Code (VS Code)
- How to Whitelist IP Address on GoDaddy Hosting?
- CentOS Cannot find a valid baseurl for repo base7x86_64 yum
- Fix: This app is no longer shared with you error iPhone or iPad iOS
- How to check the version of NodeJS installed
- [Error] There was an error connecting to the apple id server
- Find Restroom Near Me - Closest Toilet Nearby Using Your Current Location
- How to install xz data compression software using Brew
- Steps to Delete or Deactivate Instagram Account
- How to enable line numbers in IntelliJ
- How to display line numbers in Terminal while displaying content of a file
- Unzip a Zip file from Terminal Command
- Install SonarLint on Visual Studio Code
- How to Scan iPhone for Virus? Is Antivirus it really required?
- 100+ SEO Tips to improve your website search ranking
- [fix] Editor could not be opened unexpected error: File is a directory (VS Code)
- How to Fix Spelling Errors on Microsoft Word for Mac
- How to convert byte array to String [Kotlin]
- [Fix] Error 1020 Cloudflare: Access was denied
- Command: How to scp a file to remote server location?
- What is an Authorization Code Grant? OAuth 2.0
- How to Connect to AWS Windows EC2 UI Instance from M1 Mac (Updated 2022)
- How to replace with space
- VS Code Remove Unused Imports Keyboard Shortcut
- How to rerun last command in Zsh shell
- Display Seconds in Digital Clock on Mac Menu Bar [macOS] - MacOS
- Installing Google Cloud macOS SDK - Google
- How to delete all text after a character or string in Notepad++ - NotepadPlusPlus
- bash: netstat: command not found - Bash
- Hide Navigation Bar from Android Screen Activity - Android
- Java Read and Write Properties file with Examples - Java
- Error:The SDK Build Tools revision (XX.X.X) is too low for project. Minimum required is XX.X.X - Android
- How to find the installed version of Microsoft Teams - Teams