'grep' is not recognized as an internal or external command

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
3 min

banner

# 'grep' is not recognized as an internal or external command

The error "'grep' is not recognized as an internal or external command, operable program or batch file" occurs when we use the grep command on Windows.

To solve the error, use the findstr command to search for patterns of text in files or use Git Bash.

grep is not recognized as internal or external command

The findstr command is the Windows equivalent of the Unix grep command.

Here is an example that searches for the string "app" in a file called my_file.txt.

cmd
findstr "app" my_file.txt

running findstr command windows

You can look at other examples of using findstr in this section of the docs.

Alternatively, you can run the grep command in Git Bash by installing git on your Windows machine.

If you already have git installed, you can search for Git Bash and use the grep command, otherwise, you have to install git first.

# Running the grep command in Git Bash

To download git and be able to use Git Bash:

  1. Open the git downloads page and download the installer for Windows.
  2. Start the installer.
  3. You will be prompted to select a destination location. You can leave the default option and click Next.

git select destination location

  1. You will be prompted to select components on the next screen. Leave the default options and click Next.

git select components

  1. Click Next on the screen that prompts you to "Select Start Menu Folder".

  2. On the next screen, you can choose the default editor for Git, e.g. Notepad, Notepad++ or any other editor you prefer.

choose git default editor

  1. On the "Adjust the name of the initial branch in new repositories screen", click Next.

adjust name of initial branch

  1. On the "Adjust your PATH environment" screen, make sure you have the default option of "Git from the command line and also from 3rd-party software" option selected and click "Next".

adjust your path environment

  1. For all the remaining screens, leave the default option selected and click Next.
  2. Lastly, click on the Install button to install git.

Once you have git installed, click on the Search field, type "Git Bash" and start the application.

search for git bash

Now you can use the grep command directly in Git Bash.

GitBash
# ๐Ÿ‘‡๏ธ Search for "apple" in the file `my_file.txt` grep "apple" my_file.txt # ๐Ÿ‘‡๏ธ Search for "apple" recursively in all directories grep -r "apple" *

using grep command in git bash

If you need to open Git Bash in a specific folder:

  1. Open the folder in a new window.
  2. Right-click in Explorer.

open git bash in folder

  1. Click "Git Bash Here".

Now you can use the grep command to search for patterns of text in files.

GitBash
# ๐Ÿ‘‡๏ธ Search for "apple" in the file `my_file.txt` grep "apple" my_file.txt # ๐Ÿ‘‡๏ธ Search for "apple" recursively in all directories grep -r "apple" *

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev