Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the windows equivalent of Linux command wc -l?

I have a piece of code that is meant to send the following to the linux command line:

wc -l C:/inputdirectory/P*

However, I need to run this script in Windows, and am trying to find the equivalent command. I have tried

find /c /v C:/inputdirectory/P*

But this throws an error, that /v is not a valid command. Can you please tell me why this isn't working?

*note, the command itself doesn't say "inputdirectory", it has the correct directory, it's just too tedious and private to type out

like image 372
user2954167 Avatar asked Jun 21 '18 19:06

user2954167


People also ask

What is the equivalent of wc in Windows?

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

What is the equivalent of Linux command in Windows?

The which command in Linux is used to identify the location of executables. The where command is a Windows which equivalent in a command-line prompt (CMD). In a Windows PowerShell the alternative for the which command is the Get-Command utility.

Does Windows have curl or wget?

to be honest.. wget and curl aren't really built-in commands. Windows 8.1, October 2017 - curl and wget are both included in PowerShell, as this shows: stackoverflow.com/questions/33364752/…

Does Windows have a curl command?

Invoke curl.exe from a command window (in Windows, click Start > Run and then enter "cmd" in the Run dialog box). You can enter curl --help to see a list of cURL commands.

What is the wc command in Linux?

The wc command is a small application. It’s one of the core Linux utilities, so there is no need to install it. It’ll already be on your Linux computer. You can describe what it does in a very few words. It counts the lines, words, and bytes in a file or selection of files and prints the result in a terminal window.

Do Linux and windows have the same commands?

But Windows and Linux have commands with the same name as well. SNo. 1. 2. 3. 4. 5. 6. 7. 8. 9. command /? 10. 11. 12. 13. 14. 15. To write in to files. 16. To leave the terminal/command window. 17. To format a drive/partition.

What is the Windows equivalent of find /C /V?

active oldest votes. 65. The Linux/Unix "line count" command, wc -l, has a Windows equivalent of find /c /v "". 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.

Does Linux have a command-line interface?

Most of us think that Linux has terminal and we can use a command-line interface only in Linux but it is just a myth. There is a PowerShell and a command prompt in windows as well where we may execute the commands easily. But Windows and Linux have commands with the same name as well. SNo.


2 Answers

Courtesy of Eryk Sun:

Try searching for "", i.e. an empty string; use only backslash as the path separator; and quote the path if it has spaces in it:

find /c /v "" "C:\inputdirectory\P*"
like image 99
user2954167 Avatar answered Nov 02 '22 07:11

user2954167


From cmd.exe (the Command Prompt / a batch file), which is obsolescent:

  • Use the accepted answer.

From PowerShell, you have two options:

  • Option A (suboptimal): Use the accepted answer too, with a small tweak:
find --% /c /v "" "C:\inputdirectory\P*"

Note: --%, the stop-parsing symbol, tells PowerShell to pass subsequent arguments through as-is to the target program (after expanding cmd-style environment-variable references such as %USERNAME%, if any).

In the case at hand, this prevents PowerShell from parsing "" and - mistakenly - neglecting to pass it through to the external target program (find.exe).

For a summary of PowerShell's highly problematic handling of quotes when calling external programs, see this answer.

Output from the above find.exe command - and, indeed, any external program, is just text, and in this case it looks something like this:

---------- PFILE1.TXT: 42

---------- PFILE2.TXT: 666

...

While this output is easy to grasp for a human observer, it makes subsequent programmatic processing cumbersome, because text parsing is required.

Using a PowerShell-native command (cmdlet), as described below, offers more flexibility, because PowerShell commands typically emit objects with typed properties, which greatly facilitates subsequent processing.


  • Option B (preferred): Use PowerShell's own Measure-Object cmdlet with the -Line switch:

Note: While this command is more verbose than the find solution, it ultimately offers more flexibility due to outputting objects with typed properties, which greatly facilitates subsequent processing; additionally, PowerShell's sophisticated output-formatting system offers user-friendly default representations.

Get-Item -Path "C:\inputdirectory\P*" -PipelineVariable file | ForEach-Object {
  Get-Content -LiteralPath $file |
    Measure-Object -Line |
      Select-Object @{ Name='File'; Expression={ $file } }, Lines
}

The above outputs objects that have a .File and .Lines property each, which PowerShell prints as follows by default:

File                         Lines
----                         -----
C:\inputdirectory\Pfile1.txt    42
C:\inputdirectory\Pfile2.txt   666
...

In addition to a nicer presentation of the output, the object-oriented nature of the output makes it easy to programmatically process the results.

For instance, if you wanted to limit the output to those files whose line count is 100 or greater, pipe to the following Where-Object call to the above command:

... | Where-Object Lines -ge 100

If you (additionally) wanted to sort by highest line count first, pipe to the Sort-Object cmdlet:

... | Sort-Object -Descending Lines
like image 35
mklement0 Avatar answered Nov 02 '22 09:11

mklement0