counting number of directories in a specific directory

后端 未结 15 2174
被撕碎了的回忆
被撕碎了的回忆 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:13

    I think the easiest is

      ls -ld images/* | wc -l
    

    where images is your target directory. The -d flag limits to directories, and the -l flag will perform a per-line listing, compatible with the very familiar wc -l for line count.

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

    Some useful examples:

    count files in current dir

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

    count dirs in current dir

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

    count files and dirs in current dir

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

    count files and dirs in in one subdirectory

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

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

    When I have tried with ls instead /bin/ls the -A parameter do not list implied . and .. NOT WORK as espected. When I use ls that show ./ and ../ So that result wrong count. SOLUTION : /bin/ls instead ls

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

    No of directory we can find using below command

    ls -l | grep "^d" | wc -l

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

    Best way to navigate to your drive and simply execute

    ls -lR | grep ^d | wc -l
    

    and to Find all folders in total, including subdirectories?

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

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

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

    Cheers!

    0 讨论(0)
  • 2020-12-22 21:17
    find . -mindepth 1 -maxdepth 1 -type d | wc -l
    

    For find -mindepth means total number recusive in directories

    -maxdepth means total number recusive in directories

    -type d means directory

    And for wc -l means count the lines of the input

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

    Best way to do it:

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

    This gives you the perfect count.

    0 讨论(0)
提交回复
热议问题