Count number of specific file type of a directory and its sub dir in mac

五迷三道 提交于 2019-12-20 09:11:29

问题


I use ls -l *.filetype | wc -l but it can only find files in current directory.
How can I also count all files with specific extension in its sub dirs?
Thank you very much.


回答1:


You can do that with find command:

find . -name "*.filetype" | wc -l



回答2:


The following compound command, albeit somewhat verbose, guarantees an accurate count because it handles filenames that contain newlines correctly:

total=0; while read -rd ''; do ((total++)); done < <(find . -name "*.filetype" -print0) && echo "$total"

Note: Before running the aforementioned compound command:

  1. Firstly, cd to the directory that you want to count all files with specific extension in.
  2. Change the filetype part as appropriate, e.g. txt

Demo:

To further demonstrate why piping the results of find to wc -l may produce incorrect results:

  1. Run the following compound command to quickly create some test files:

    mkdir -p ~/Desktop/test/{1..2} && touch ~/Desktop/test/{1..2}/a-file.txt && touch ~/Desktop/test/{1..2}/$'b\n-file.txt'
    

    This produces the following directory structure on your "Desktop":

    test
    ├── 1
    │   ├── a-file.txt
    │   └── b\n-file.txt
    └── 2
        ├── a-file.txt
        └── b\n-file.txt
    

    Note: It contains a total of four .txt files. Two of which have multi-line filenames, i.e. b\n-file.txt.

    On newer version of macOS the files named b\n-file.txt will appear as b?-file.txt in the "Finder", i.e. A question mark indicates the newline in a multi-line filename

  2. Then run the following command that pipes the results of find to wc -l:

    find ~/Desktop/test -name "*.txt" | wc -l
    

    It incorrectly reports/prints:

    6

  3. Then run the following suggested compound command:

    total=0; while read -rd ''; do ((total++)); done < <(find ~/Desktop/test -name "*.txt" -print0) && echo "$total"
    

    It correctly reports/prints:

    4



来源:https://stackoverflow.com/questions/12656427/count-number-of-specific-file-type-of-a-directory-and-its-sub-dir-in-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!