Main Tutorials

Grep for Windows – findstr example

linux-logo

I love grep command on Linux, it helped to search and filter strings easily, always wonder what is the equivalent tool on Windows, and found this findstr recently.

In this article, I will share some of my favorite “grep” examples on Linux, and how to “port” it to Windows with “findstr” command.

1. Filter a result

1.1 Classic example to filter a listing result.


#Linux
$ ls -ls | grep mkyong

#Windows
c:\> dir | findstr mkyong

1.2 Add ignore case, and filter the listing result with multiple strings.


#Linux - Need '-E' option and Uses "|" to separate multiple search strings.
$ ls -ls | grep -iE "mkyong|music"

#Windows - Use spaces to separate multiple search strings
c:\> dir | findstr -i "mkyong music"

2. Search a File

2.1 Search matched string in a file.


#Linux 
$ grep mkyong test.txt

#Windows
c:\> findstr mkyong test.txt

2.2 Counting the number of matches.


#Linux
$ grep -c mkyong test.txt

#Windows - Piped with find /c command.
c:\> findstr -N "mkyong" test.txt | find /c ":"

3. Search a list of files

3.1 Search matched string in a list of files.


#Linux
$ grep mkyong -lr /path/folder

#Windows
c:\> findstr /M mkyong c:\folder\*

* (grep) -l , (findstr) /M = print only name of files containing matches.

4. Help

4.1 The most powerful command ~


#Linux 
$ grep --help 
$ man grep 

#Windows
c:\> findstr -?
Note
Do you have other examples? Does share it below, thanks.

References

  1. 15 Practical Grep Command Examples In Linux / UNIX
  2. Wikipedia : Findstr
  3. Grep for Windows Using FINDSTR

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
17 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Abdul Mohsin
6 years ago

Thanks man, It was very Helpful 🙂

Christian Brudevoll
6 years ago

How do I exclude lines starting with a space?
Unix: ls -l | egrep -v “^ “

manju
2 years ago

Thank you

Francesco Urbano
2 years ago

Very useful. Thanks!

Last edited 2 years ago by Francesco Urbano
Amit Kharade
2 years ago

Thanks, this is very useful.

Last edited 2 years ago by Amit Kharade
tharindu sathsara
3 years ago

Thank you . I found this article after very tired of finding about grep. but now my problem solved thank you..

Möhre
6 years ago

what is with:
find myDirectory -type f | grep ‘.txt$’ >nl.files

I dont know how to fix the find command… Any help?

Schtick
6 years ago
Reply to  Möhre

dir /B d:mydir*.tmp > nl.files
dir /B c:mydir | findstr tmp
But some files extensions may be displayed as uppercase and some as lowercase, so if not result when you type tmp, try type TMP

Schtick
6 years ago
Reply to  Schtick

Of course after “:” must be a slash to the left “”

gene8
8 years ago

Thanks, it is very helpful

Radjammin
9 years ago

syntax on powershell commands is still too thick. Bash > Powershell Come on Redmond, you guys are great at copying. Lets get crackin’

Asshiah
9 years ago

search the string in current directory and its subdirectories while only displaying file names
#Windows
c:> findstr /M /S mkyong

Asshiah
9 years ago
Reply to  Asshiah

Rather its:
findstr /M /S mkyong *.*

You could add /I to make a case-insensitive search

Caleb Cushing
10 years ago

Worth noting that some unix basics work in Powershell out of the box, like `ls`. Don’t know if findstr works in powershell though, and the powershell equivalent to grep is painful for me to remember.

Vartika
2 years ago

I tried to execute these commands in windows 10, but its showing that,
‘c:\’ is not recognised as an internal or external command.
Could anyone guide me about this.

SylwesterB
1 year ago
Reply to  Vartika

Try this: findstr mkyong *

tytoriddle2
10 years ago

doesnt work for me