123

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?

Niall
  • 3,494
  • 4
  • 18
  • 20
  • 1
    You could also use the GnuWin32 tools and actually have `wc` (well, `wc.exe` ...) http://gnuwin32.sourceforge.net/ – ivanivan Jan 15 '19 at 19:03

4 Answers4

149

The Linux/Unix "line count" command, wc -l, has a Windows equivalent 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 (%%f) in batch files.


PowerShell

A working PowerShell equivalent is Measure-Object -line — some additional formatting is required though, e.g. a directory listing for simplicity:

(ls | Measure-Object -line).Lines
AcK
  • 105
  • 5
Niall
  • 3,494
  • 4
  • 18
  • 20
  • 1
    Thanks for sharing. Created a gist for reference - https://gist.github.com/a4ede7054b2ce5ee2708dd49bbd5a1a1 – maheshwaghmare Jun 17 '19 at 13:56
  • find.exe lives in "Windows\System32" if your Path has another 'find' you will need to profile the path. – ChetS Jun 28 '21 at 15:20
  • This worked for me using triple quote as in [this answer](https://superuser.com/a/1269470/1268619) i.e. `ls | find /c /v """"""` – pjpscriv Nov 03 '22 at 02:31
  • `(ls | Measure-Object -line).Lines`, exactly what I was looking for ;), and @pjpscriv, for me `ls | find /c /v """"""` does not work since it does not count only files but also all empty lines and tables header around, while `Measure-Object` method actually gives me 28 where I have 28 files, `findstr` method gives me 37 (file lines + extra lines). – gluttony Aug 11 '23 at 09:32
47

In PowerShell, to obtain a line count of all your java files:

type *.java | Measure-Object -line
crestor
  • 571
  • 4
  • 2
  • 4
    welcome to superuser: - the question was about CMD not powershell, if you can answer the OP please edit your answer. these may help [answer], [ask], [help] again welcome to superuer and i hope you return, thankyou – mic84 Aug 06 '18 at 09:52
  • 23
    Thanks @mic84. The question is about Windows command line. And Microsoft is trying to replace CMD with PowerShell: https://www.networkworld.com/article/3143196/windows/microsoft-is-replacing-the-cmd-prompt-with-powershell.html So this is a quite useful answer. – crestor Aug 07 '18 at 10:41
  • This inspired me to try adding this to my `Microsoft.PowerShell_profile.ps1` file: `function wc () { Measure-Object -c -l -w }` (it did not work as expected.) – MarkHu Jul 14 '23 at 22:16
2

Use "Count":

(Get-AppXPackage|?{!$_.InstallLocation}).Count
(ls).Count
-1

findstr /n /r .

This is a limited regex, so /n shows the line number of the match, and "." is match any char.

  • 7
    The question isn’t how to display the file with line numbers; the question is how to determine *the number of lines* in the file. – Scott - Слава Україні Jul 16 '19 at 00:29
  • 1
    Look at the number of the last line, and that's how many lines are in the file. The "find" command works, too, but maybe findstr will be useful in some cases. I have found it to be. – Gary Gatten Jul 19 '19 at 19:16
  • 3
    That’s not a very user-friendly answer, especially given that we’ve received a couple of other answers that are more direct.  But, if that’s your answer, then please [edit] your answer to make it clearer and more complete. – Scott - Слава Україні Jul 19 '19 at 19:48
  • 2
    the point is for automation scripting. No scripts will "look" at the line number to store the number of lines in a variable for further computation. And flooding the screen with unnecessary information lines and wasting CPU cycles are definitely not good – phuclv Jul 26 '22 at 01:42