70

While having a cygwin installed in windows gives most of unix command, still i was wondering how to search multiple filetypes in one command using windows "find" command.
ie: find . -name *.cpp -o -name *.h -o -name *.java

The above command gives me a list of all cpp, h & java, what will be the equivalent using the windows find?

Soumen
  • 1,006
  • 2
  • 12
  • 19

2 Answers2

105

This will locate all files with the given extensions in the current working directory and all subdirectories:

dir *.cpp *.h *.java /b/s

See https://technet.microsoft.com/en-us/library/cc755121.aspx for more info on using dir.

JAB
  • 20,783
  • 6
  • 71
  • 80
  • what this is supposed to do is search a sample "text" inside all the files specified. I dont want to do this, i want it to list all the files in the current and sub-directory with file extension cpp, h or java. – Soumen Jun 10 '11 at 06:57
  • 3
    also this command is not working in windows7 somehow .. or maybe i am wrong. Also i found `tree` might be better alternative than `find` in windows. – Soumen Jun 10 '11 at 06:58
  • 1
    @Soumen: Then don't say 'windows "find" command', but 'the Windows equivalent to unix's "find" command'. Anyway, updated my answer with something that should be closer to what you want. (If I'm not mistaken, `tree` doesn't allow you to restrict results to files with specific extensions.) – JAB Jun 10 '11 at 14:03
  • thanks, this is what i was looking for. And you are right about the question title also. – Soumen Jun 13 '11 at 11:24
  • tree documentation http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/tree.mspx?mfr=true – John Zumbrum Jan 06 '14 at 23:16
  • @JohnZ `tree` still doesn't allow you to restrict results to files with specific extensions (though you could of course parse the output from `tree` and remove those lines with files that do not match a certain pattern or regular expression, but that would require an additional step in the process). – JAB Jan 07 '14 at 14:19
  • It's more like `locate` than `find`... It wasn't even able to locate a file I was looking for until I found it myself! – Jaffa Jul 08 '14 at 12:57
  • 3
    And if you want to emulate the very common `find . -name \*.cpp | wc -l` *nix command idiom, you can use `dir *.cpp /b/s | find /c /v ""`. See [Raymond Chen's post](http://blogs.msdn.com/b/oldnewthing/archive/2011/08/25/10200026.aspx) for details. – ddevienne Apr 21 '15 at 08:41
  • 1
    How to narrow this down to subdir? `find . -name "*Test.php" src` – ioleo Jan 29 '16 at 11:18
  • @loostro For your specific case, it would be `dir src\*Test.php /b/s`, as noted in the Microsoft documentation. – JAB Jan 29 '16 at 14:50
  • `dir *.exe /b/s` gives me `dir : Second path fragment must not be a drive or UNC name.` Windows 10 version 1903. – John Freeman Jul 06 '19 at 01:34
  • @JohnFreeman that syntax should still be valid based off of recent docs, so I'll take a look. – JAB Jul 06 '19 at 02:33
  • @Soumen On a Windows 10 x64 1903 machine, this does a text search as other comments have noted...so I'm confused as to why other comments here are saying that it works, now that you've updated the question title. Am I misunderstanding what you actually wanted? I came here looking for a way to find files with an extension of ".abc", not files with ".abc" in the name :S – Kenny83 Jul 12 '19 at 16:47
  • @ddevienne, your link doesn't work as of February 2023. – Dr Phil Feb 15 '23 at 16:39
  • 2
    @DrPhil I tracked it down to https://devblogs.microsoft.com/oldnewthing/20110825-00/?p=9803. Just from the old URL and a little time. You could also have done that :) – ddevienne Feb 16 '23 at 08:14
-3

findstr /p /s /i .

above command searches for the given text in current directories and sub directories. /n will print the line numbers too.

Sreedhar GS
  • 2,694
  • 1
  • 24
  • 26