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] Error 1020 Cloudflare: Access was denied
- What is an Authorization Code Grant? OAuth 2.0
- Fix: psql: error: connection to server at localhost (::1) port 5432 failed: FATAL: database does not exist
- 100+ SEO Tips to improve your website search ranking
- How to submit website to dmoz directory
- How to check the version of NodeJS installed
- How to enter Rockstar Activation Code?
- Background task activation is spurious error - Windows 10, Office 2016, Office 365
- Angular CLI ng Error: You need to specify a command before moving on. Use --help to view the available commands
- cURL HTTP GET request command examples
- Fix - JioCinema Something Went Wrong Please Try Again Error 8001 while streaming IPL Cricket Live
- How to remove password from pdf file
- How to identify the version of IntelliJ
- Command: How to scp a file to remote server location?
- How to add Widgets to MacBook Desktop
- Setting up RSS feeds notifications within Outlook
- Fix - Website Connection timed out Error Code 522 (Cloudflare)
- How to fix: Please check your network connection (Retry)
- Fix: Could not connect to Redis at 127.0.0.1:6379: Connection refused
- Accept Only 0-9 Numbers RegEx Example
- How to create a New Project in Visual Studio Code (VS Code)
- Top 10 emerging breakthrough trending technologies
- Free Unlimited Calls from MTNL & BSNL Landlines from 1st May 2015
- [Error] zsh: command not found: mvn
- How to Scan iPhone for Virus? Is Antivirus it really required?
- @Configuration - A look at Spring Annotations in Depth - Java
- Fix - ssh-copy-id no identities found error - Linux
- Possible outages message Google Webmaster tool - Google
- A Deep Dive into HTML Header Tags (H1-H6) - CSS
- Generate Facebook Android SDK keyhash using java code - Android
- [fix] command not found curl - cURL
- AlertDialog with image using ImageView Example - Android
- [Error] Microsoft Teams: We're sorryβwe've run into an issue. - Microsoft