Windows – the Windows equivalent of “wc -l”

command linewindows

What is the Windows equivalent of the Linux/Unix command wc -l?

Basically, how do you count the number of lines output from a command on the Windows command line?

Best Answer

The Linux/Unix "line count" command, wc -l, has a Windows equivalent of find /c /v "".

How does this work?

According to Raymond Chen of the The Old New Thing, this functions as such since

It is a special quirk of the find command that the null string is treated as never matching.

The inverted (/v) count (/c) thus effectively counts all the lines; hence, the line count.

Example usage

To count the number of modified files in a subversion working copy:

svn status -q | find /c /v ""

Such a command can be used to mark a build as "dirty" if the count is not 0, i.e., there are uncommitted changes in the working copy.

To obtain a line count of all your Java files:

(for /r %f in (*.java) do @type "%f") | find /c /v ""

The command find /c /v "" can also be added to a batch file if required.  Remember to duplicate the % characters in batch files.


PowerShell

A working PowerShell equivalent is Measure-Object -line with some additional formatting required, e.g., (directory listing for simplicity),

(ls | Measure-Object -line).Lines