Recursively counting files in a Linux directory

后端 未结 21 1017
既然无缘
既然无缘 2020-11-28 17:17

How can I recursively count files in a Linux directory?

I found this:

find DIR_NAME -type f ¦ wc -l

But when I run this it returns

相关标签:
21条回答
  • 2020-11-28 17:25

    You can use

    $ tree
    

    after installing the tree package with

    $ sudo apt-get install tree
    

    (on a Debian / Mint / Ubuntu Linux machine).

    The command shows not only the count of the files, but also the count of the directories, separately. The option -L can be used to specify the maximum display level (which, by default, is the maximum depth of the directory tree).

    Hidden files can be included too by supplying the -a option .

    0 讨论(0)
  • 2020-11-28 17:27

    This should work:

    find DIR_NAME -type f | wc -l
    

    Explanation:

    • -type f to include only files.
    • | (and not ¦) redirects find command's standard output to wc command's standard input.
    • wc (short for word count) counts newlines, words and bytes on its input (docs).
    • -l to count just newlines.

    Notes:

    • Replace DIR_NAME with . to execute the command in the current folder.
    • You can also remove the -type f to include directories (and symlinks) in the count.
    • It's possible this command will overcount if filenames can contain newline characters.

    Explanation of why your example does not work:

    In the command you showed, you do not use the "Pipe" (|) to kind-of connect two commands, but the broken bar (¦) which the shell does not recognize as a command or something similar. That's why you get that error message.

    0 讨论(0)
  • 2020-11-28 17:29

    Combining several of the answers here together, the most useful solution seems to be:

    find . -maxdepth 1 -type d -print0 |
    xargs -0 -I {} sh -c 'echo -e $(find "{}" -printf "\n" | wc -l) "{}"' |
    sort -n
    

    It can handle odd things like file names that include spaces parenthesis and even new lines. It also sorts the output by the number of files.

    You can increase the number after -maxdepth to get sub directories counted too. Keep in mind that this can potentially take a long time, particularly if you have a highly nested directory structure in combination with a high -maxdepth number.

    0 讨论(0)
  • 2020-11-28 17:30

    If you want to know how many files and sub-directories exist from the present working directory you can use this one-liner

    find . -maxdepth 1 -type d -print0 | xargs -0 -I {} sh -c 'echo -e $(find {} | wc -l) {}' | sort -n
    

    This will work in GNU flavour, and just omit the -e from the echo command for BSD linux (e.g. OSX).

    0 讨论(0)
  • 2020-11-28 17:30

    You can use the command ncdu. It will recursively count how many files a Linux directory contains. Here is an example of output:

    It has a progress bar, which is convenient if you have many files:

    To install it on Ubuntu:

    sudo apt-get install -y ncdu
    

    Benchmark: I used https://archive.org/details/cv_corpus_v1.tar (380390 files, 11 GB) as the folder where one has to count the number of files.

    • find . -type f | wc -l: around 1m20s to complete
    • ncdu: around 1m20s to complete
    0 讨论(0)
  • 2020-11-28 17:30

    I have written ffcnt to speed up recursive file counting under specific circumstances: rotational disks and filesystems that support extent mapping.

    It can be an order of magnitude faster than ls or find based approaches, but YMMV.

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