Findstr: Find Specific String In Files With Windows Command (Grep Alternative)

itechtics featured fallbackitechtics featured fallback

Most of the users who use Linux are very comfortable with using command-line. And one of the basic requirement is to find specific strings within specific file or files or even from the output of a particular command. Grep is a command-line option used to find a specific string from inside a file or multiple files or from an output of a command but it can be used only in Linux. For Windows, the grep alternative is findstr.

Since Windows users are not used to use command-line for smaller things, most of the users don’t know how to find a specific string in files using Windows command-line or even PowerShell. In this article, we will discuss about how to use findstr (equivalent of Grep in Windows) in command prompt and also how to use the find function using PowerShell. Let’s discuss about different scenarios which can be used in real-world situations.

Filter an output of a command Using Command Prompt

If you want to filter the results of a command, you can use | findstr “string_to_find”

For example, I mostly use netstat for checking the connections being made on my computer. If I want to check which app or IP address is connected to a specific port, I’ll use the following command:

netstat | findstr “imaps”

Findstr filtering imaps ports
Findstr filtering imaps ports

This will show me only secure imap ports opened on my computer.

Search for a specific string inside a single file Using Command Prompt

Effective Strategies for Removing M...
Effective Strategies for Removing Malware from Your Computer

The command for this purpose is:

findstr “string_to_find” “file_name”

For example,

findstr “reader” “new 1.txt”

You can also give full path of the file if it’s not in the same directory as opened in command prompt.

find text string in a file
find text string in a file

Search for a specific string in a folder using Findstr

You can also specify a folder for finding a specific text string in multiple files.

findstr /M “reader” “C:\Users\Usman\Desktop\*”

This will give a list of all files with full path containing the text string “reader”. If you don’t specify /M, the output will show the exact text string along with the file name where it found the string.

Finding a string from folder
Finding a string from folder

You can go through all the switches you can use with the command here.

This command can be useful in many cases especially when I am creating a log of network activities and have to find a specific thing from the log. What do you do with this filter command “findstr”?

If you liked this post, Share it on:

Get Updates in Your Inbox

Sign up for the regular updates and be the first to know about the latest tech information

Usman Khurshid
Usman Khurshid is a seasoned IT Pro with over 15 years of experience in the IT industry. He has experience in everything from IT support, helpdesk, sysadmin, network admin, and cloud computing. He is also certified in Microsoft Technologies (MCTS and MCSA) and also Cisco Certified Professional in Routing and Switching.

7 comments

  • OKRickety
    OKRickety

    The author seems to have forgotten the article was also to discuss “how to use the find function using PowerShell”.

  • NPadmanabhan
    NPadmanabhan

    Thanks for giving the grep equivalent in windows. I wish you come up with equivalents of all commonly used unix commands and compile in a file so that users like me can use it as a reference book. Thanks once again.

    Padmanabhan

    • Usman Khurshid
      A
      Usman Khurshid

      What other commands do you normally use in Linux and Unix that are not available in Windows?

  • Beau

    I’ve been trying to find filenames that start with a certain string.
    I started by finding all files within a certain directory which contains a bunch of sub directories and such by ‘dir * /s/b | findstr “.*\..*”‘ I pipped the results to a DirectoryListing.txt file to store all the paths.
    I also have a List.txt file which contains strings of filenames, ie: ‘12345’. Each filename is on its own line.
    I then tried to find all filenames that start with strings contained within the List.txt from my DirectoryListing.txt file. In doing so I got filenames that didn’t start with my searched string. If I took my string ‘12345’, I would expect to get back filenames that start with ‘12345’, but I also received filenames that included a hyphen ’11-12345′. Like in your example where you FINDSTR ‘reader’ it returned “adobe-reader-…”.
    This is how I am trying to parse the file: For /F “Delims=|” %%X In (‘FINDSTR /IRC:”\” “%destination%\%DirList%”‘)
    %%A is the string to find, ie: ‘12345’
    %DirList% is my DirectoryListing.txt

    My question is how would I be able to use FINDSTR to find filenames that start with a certain string and not have a ‘-‘ hyphen before the string?

  • teo

    Do I understand correctly: findstr matches a STRING, not a regular expression pattern, right?

    And you call that an “equivalent to grep”??

    • Usman Khurshid
      A
      Usman Khurshid

      The closest alternative of grep in Windows is findstr. We can’t compare all the features of each command but the basic function of the commands is the same.

  • dasra

    I am trying to filter a gitbranch name so I used git branch | findstr “*” it is outputting a branch name but including the * sign. I was wondering if there is any way to select the exact name of the branch and save it to the string.
    SPecially for branches lik rel-0.1 it is not working.

    I am trying to convert git branch | grep \* | cut -d ‘ ‘ -f2 | git pull origin from bash script to
    >git branch | findstr “*” | git pull origin but anytime the branch name is like rel-0.1 it does not work but for others it is working.
    The goal is to pull origin from the current selected branch.

Leave a Reply to NPadmanabhan (Cancel Reply)