Recursively counting files in a Linux directory

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 the following error.

find: paths must precede expression: ¦


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.


    对于当前目录:

    find . -type f | wc -l
    

    If you want a breakdown of how many files are in each dir under your current dir:

    for i in $(find . -maxdepth 1 -type d) ; do 
        echo -n $i": " ; 
        (find $i -type f | wc -l) ; 
    done
    

    That can go all on one line, of course. The parenthesis clarify whose output wc -l is supposed to be watching ( find $i -type f in this case).

    链接地址: http://www.djcxy.com/p/13722.html

    上一篇: BufferedInputStream字符串转换?

    下一篇: 递归计数Linux目录中的文件