counting number of directories in a specific directory

后端 未结 15 2176
被撕碎了的回忆
被撕碎了的回忆 2020-12-22 21:06

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

find /directory/ -maxdept         


        
相关标签:
15条回答
  • 2020-12-22 21:25

    Get a count of only the directories in the current directory

    echo */ | wc

    you will get out put like 1 309 4594

    2nd digit represents no. of directories.

    or

    tree -L 1 | tail -1

    0 讨论(0)
  • 2020-12-22 21:25

    Run stat -c %h folder and subtract 2 from the result. This employs only a single subprocess as opposed to the 2 (or even 3) required by most of the other solutions here (typically find plus wc).

    Using sh/bash:

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

    Using csh/tcsh:

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


    Explanation: stat -c %h folder prints the number of hardlinks to folder, and each subfolder under folder contains a ../ entry which is a hardlink back to folder. You must subtract 2 because there are two additional hardlinks in the count:

    1. folder's own self-referential ./ entry, and
    2. folder's parent's link to folder
    0 讨论(0)
  • 2020-12-22 21:25

    The best answer to what you want is

    echo `find . -maxdepth 1 -type d | wc -l`-1 | bc
    

    this subtracts one to remove the unwanted '.' directory that find lists (as patel deven mentioned above).

    If you want to count subfolders recursively, then just leave off the maxdepth option, so

    echo `find . -type d | wc -l`-1 | bc
    

    PS If you find command substitution ugly, subtracting one can be done as a pure stream using sed and bc.

    Subtracting one from count:

    find . -maxdepth 1 -type d | wc -l | sed 's/$/-1\n/' | bc
    

    or, adding count to minus one:

    find . -maxdepth 1 -type d | wc -l | sed 's/^/-1+/' | bc
    
    0 讨论(0)
  • 2020-12-22 21:28

    Using zsh:

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

    The N is a nullglob, / makes it match directories, # counts. It will neatly cope with spaces in directory names as well as returning 0 if there are no directories.

    0 讨论(0)
  • 2020-12-22 21:37

    A pure bash solution:

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

    If you also want to count the hidden directories:

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

    Note that this will also count links to directories. If you don't want that, it's a bit more difficult with this method.


    Using find:

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

    The trick is to output an x to stdout each time a directory is found, and 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 all safe wrt to funny characters that can appear in file names (spaces, newlines, glob characters, etc.).

    0 讨论(0)
  • 2020-12-22 21:37

    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 讨论(0)
提交回复
热议问题