counting number of directories in a specific directory

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

    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

提交回复
热议问题