Counting the number of directories in a specific directory

How to count the number of folders in a specific directory. I use the following command, but it always provides an extra.

find /directory/ -maxdepth 1 -type d -print| wc -l 

For example, if I have 3 folders, this command provides 4. If it contains 5 folders, the command provides 6. Why is this?

+72
linux bash
Jul 15 '13 at 6:33
source share
13 answers

find also prints the directory itself:

 $ find .vim/ -maxdepth 1 -type d .vim/ .vim/indent .vim/colors .vim/doc .vim/after .vim/autoload .vim/compiler .vim/plugin .vim/syntax .vim/ftplugin .vim/bundle .vim/ftdetect 

Instead, you can test the children of the directory and not go down at all:

 $ find .vim/* -maxdepth 0 -type d .vim/after .vim/autoload .vim/bundle .vim/colors .vim/compiler .vim/doc .vim/ftdetect .vim/ftplugin .vim/indent .vim/plugin .vim/syntax $ find .vim/* -maxdepth 0 -type d | wc -l 11 $ find .vim/ -maxdepth 1 -type d | wc -l 12 

You can also use ls :

 $ ls -l .vim | grep -c ^d 11 $ ls -l .vim total 52 drwxrwxr-x 3 anossovp anossovp 4096 Aug 29 2012 after drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 autoload drwxrwxr-x 13 anossovp anossovp 4096 Aug 29 2012 bundle drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 colors drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 compiler drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 doc -rw-rw-r-- 1 anossovp anossovp 48 Aug 29 2012 filetype.vim drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 ftdetect drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 ftplugin drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 indent drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 plugin -rw-rw-r-- 1 anossovp anossovp 2505 Aug 29 2012 README.rst drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 syntax $ ls -l .vim | grep ^d drwxrwxr-x 3 anossovp anossovp 4096 Aug 29 2012 after drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 autoload drwxrwxr-x 13 anossovp anossovp 4096 Aug 29 2012 bundle drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 colors drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 compiler drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 doc drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 ftdetect drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 ftplugin drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 indent drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 plugin drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 syntax 
+74
Jul 15 '13 at 6:41
source share

Get the number of directories in the current directory only

echo */ | wc

you will choose as 1 309 4594

2nd digit does not represent. directories.

or

tree -L 1 | tail -1

+45
Apr 09 '14 at 8:44
source share
 find . -mindepth 1 -maxdepth 1 -type d | wc -l 

For find -mindepth means the total number returned in directories

-maxdepth means the total number returned in directories

-type d means directory

And for wc -l means counting input lines

+17
Dec 20 '15 at 21:43
source share

The best way to go to your drive and just do

 ls -lR | grep ^d | wc -l 

and find all folders in total, including subdirectories?

 find /mount/point -type d | wc -l 

... or find all the folders in the root directory (not including subdirectories)?

 find /mount/point -maxdepth 1 -type d | wc -l 

Hurrah!

+12
Apr 30 '14 at 14:02
source share

Run stat -c %h folder and stat -c %h folder 2 from the result. In this case, only one subprocess is used, unlike the 2 (or even 3) required by most other solutions here (usually find plus wc ).

Using sh / bash:

cnt = $ (('stat -c% h folder' - 2))
echo $ cnt # 'echo' is a built-in sh / bash function, not an additional process

Using csh / tcsh:

@cnt = 'stat -c% h folder' - 2
echo $ cnt # 'echo' is a built-in csh / tcsh function, not an additional process


Explanation: stat -c %h folder prints the number of hard links to the folder, and each subfolder in the folder contains an .. / entry, which is a hard link to the folder. You have to subtract 2, because there are two additional hard links in the count:

  1. own self-referencing folder. / record, and
  2. folder parent link to folder
+9
Jul 26 '16 at 0:41
source share

I think the easiest

  ls -ld images/* | wc -l 

where images is your target directory. The -d flag restricts directories, and the -l flag will list per line, compatible with the very familiar wc -l for the number of lines.

+9
Aug 07 '16 at 23:20
source share

If you only have directories in the folder and no files, this does this:

 ls | wc -l 
+5
Jan 20 '19 at 9:54
source share

Pure bash solution:

 shopt -s nullglob dirs=( /path/to/directory/*/ ) echo "There are ${#dirs[@]} (non-hidden) directories" 

If you also want to count hidden directories:

 shopt -s nullglob dotglob dirs=( /path/to/directory/*/ ) echo "There are ${#dirs[@]} directories (including hidden ones)" 

Please note that this will also read directory links. If you do not want this, it is a little more complicated with this method.




Using find :

 find /path/to/directory -type d \! -name . -prune -exec printf x \; | wc -c 

The trick is to output x to stdout every time a directory is found, then use wc to count the number of characters. This will count the number of all directories (including hidden ones), excluding links.




The methods presented here are safe for funny characters that can appear in file names (spaces, newlines, globe characters, etc.).

+3
May 27 '16 at 11:02
source share

Some useful examples:

read files in the current directory

 /bin/ls -lA | egrep -c '^-' 

count dirs in the current dir

 /bin/ls -lA | egrep -c '^d' 

read files and directories in the current directory

 /bin/ls -lA | egrep -c '^-|^d' 

read files and directories in one subdirectory

 /bin/ls -lA subdir_name/ | egrep -c '^-|^d' 

I noticed a strange thing (at least in my case):

When I tried using ls instead of /bin/ls the -A option do not list implied. and.. do not list implied. and.. -A do not list implied. and.. do not list implied. and.. do not list implied. and.. do not list implied. and.. DOES NOT WORK as expected. When I use ls that show. / And ../ So the result is wrong. SOLUTION: /bin/ls instead of ls

+3
Mar 07 '18 at 17:23
source share

Using zsh :

 a=(*(/N)); echo ${#a} 

N is nullglob, / makes it match directories, # counts. It will neatly deal with spaces in directory names, and also return 0 if there are no directories.

+2
Nov 14 '16 at 10:18
source share

Count all files and subfolders, windows style:

 dir=/YOUR/PATH;f=$(find $dir -type f | wc -l); d=$(find $dir -mindepth 1 -type d | wc -l); echo "$f Files, $d Folders" 
0
Nov 17 '17 at 18:11
source share

If you want to use regular expressions, try:

 ls -c | grep "^d" | wc -l 
0
May 19 '19 at
source share

The best way to do this is:

 ls -la | grep -v total | wc -l 

It gives you the perfect score.

-one
Sep 15 '16 at 11:16
source share



All Articles