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!
- Fix NVIDIA GeForce Experience ERROR CODE 0x0003
- How to convert byte array to String [Kotlin]
- Unzip a Zip file from Terminal Command
- How to write hello world different languages syntax
- [Error] zsh: command not found: mvn
- How to know the version of OpenSSL
- [Solution] IDEA IntelliJ System.out.println function shortcut (sysout alternative for eclipse IDE)
- The default username and password for RabbitMQ
- ChatGPT Outage: Hmm...something seems to have gone wrong. Maybe try me again in a little bit.
- How to check the version of NodeJS installed
- Gmail Unable to upload because it is a folder or a package (like an application bundle or RTFD document)
- How to remove password from pdf file
- BSNL Broadband upgrades speed to minimum 2MBps for all users 512Kbps 1Mbps
- MongoDB: Failed to connect to 127.0.0.1:27017 reason: Connection refused
- Turn off Focus Mode on Mac
- Steps to Delete or Deactivate Instagram Account
- [IRCTC] Indian railways official eRail API 1.1 for developers to get train info
- CentOS Cannot find a valid baseurl for repo base7x86_64 yum
- Why I see Download pre-built shared indexes in IntelliJ
- Copy file from a remote server to current local directory system using SCP command
- [Fix] Minecraft Error: A JNI error has occurred, please check your installation and try again
- How to install Zsh shell
- Merge multiple zip files without unzipping (extracting)
- How to find someone on Instagram
- How to get an embed code from Vimeo?
- Get Button Text onClick Android App - Android
- How to show Machintosh HD icon on Mac Desktop - MacOS
- Find the location of Spotlight searched file - Mac-OS-X
- Quick way to setup AWS DynamoDB locally on macOS - AWS
- 300+ Eclipse IDE Keyboard Shortcuts for Mac - Eclipse
- Drop table using Java JDBC Template - Java
- 2020 Popular Halloween Emoji's and hashtags - Hashtags
- Share image and text Twitter using your Android Application Programatically - Twitter