246
78
Is there a command prompt grep equivalent for Windows 7? That is, I want to filter out the results of a command:
Bash use:
ls | grep root
What would it be from a Windows command prompt?
246
78
Is there a command prompt grep equivalent for Windows 7? That is, I want to filter out the results of a command:
Bash use:
ls | grep root
What would it be from a Windows command prompt?
287
Findstr sounds like what you want. I use it all the time as an approximate grep-equivalent on the Windows platform.
Another example with pipes:
C:\> dir /B | findstr /R /C:"[mp]"
19why couldn't they just call it grep! – Victor Grazi – 2015-09-08T15:15:57.820
6
@VictorGrazi They actually did at first, see this article. It was originally named qgrep - but was later renamed.
– Zitrax – 2016-02-01T14:23:15.8704The big problem with findstr
is that the regex pattern may not contain alternatives. – Wolf – 2016-06-16T13:15:40.237
The question is about Windows 7, but what other versions of Windows have findstr
by default? Since Windows XP?
@VictorGrazi To not look so similar to Linux. – neverMind9 – 2018-12-31T22:23:43.347
1What does /R
and /C
stands for here? – Revolucion for Monica – 2019-02-11T14:21:44.673
/B
: display only the folder names.
/R
: Processes search strings as regular expressions. This is the default setting.
/C
: Uses the specified text as a literal search string. – fivef – 2019-11-15T13:35:25.730
from C:, I type dir | findstr /R "Progr.F*"
and it finds my two "Program Files" directories. Why does it find them? I only gave one dot. – user74094 – 2020-02-07T09:47:35.363
Yeah that seems to be what i'm looking for. I understand there are probably limitations. I'll give it a try. – chrisjlee – 2011-06-22T21:11:51.373
45
There are several possibilities:
grep
command. There are several choices. Oft-mentioned are GNUWin32, cygwin, and unxutils. Less well known, but in some ways better, are the tools in the SFUA utility toolkit, which run in the Subsystem for UNIX-based Applications that comes right there in the box with Windows 7 Ultimate edition and Windows Server 2008 R2. (For Windows XP, one can download and install Services for UNIX version 3.5.) This toolkit has a large number of command-line TUI tools, from mv
and du
, through the Korn and C shells, to perl
and awk
. It comes in both x86-64 and IA64 flavours as well as x86-32. The programs run in Windows' native proper POSIX environment, rather than with emulator DLLs (such as cygwin1.dll
) layering things over Win32. And yes, the toolkit has grep
, as well as some 300 others.grep
commands that people have written and published. Tim Charron has a native Win32 version of a modified GNU grep, for example. There are also PowerGREP, Bare Grep, grepWin, AstroGrep, and dnGrep, although these are all GUI programs not TUI programs.find
and findstr
. The syntax is different to that of grep
, note, as is the regular expression capability.You write I don't see perl in SUA C:\Windows\SUA>dir perl* /s/b File Not Found and running e.g. c shell of SUA I don't see it in /bin – barlop – 2015-05-27T21:12:15.753
you also write that SFUA is in some ways better. In what ways? – barlop – 2016-04-24T11:17:39.533
@Kozuch unless you state how it didn't work, then your comment is not useful – barlop – 2016-04-24T11:18:03.843
cygwin worked for me nicely on Win 7. I needed grep and GnuWin32 grep did not work for me. – Kozuch – 2014-02-21T11:58:14.000
19
If PowerShell commands are allowed, use
PS C:\> Get-ChildItem | Select-String root
or short
PS C:\> ls | sls root
Be aware that the alias sls
is only defined beginning with PowerShell version 3.0. You may add an alias for less typing:
PS C:\> New-Alias sls Select-String
To run the PowerShell command directly from cmd, use
C:\>powershell -command "ls | select-string root"
2
Grep, the Powershell way. https://communary.net/2014/11/10/grep-the-powershell-way/
– Charles Burge – 2017-01-09T22:59:01.95018
In your early revision you wrote MS-DOS, there's only FIND
, as far as I know. But it's an ancient OS not used anymore.
In the Windows NT command prompt(e.g. Win2K and win XP and later, so e.g. win7,win10), you can use find
and findstr
and if you download GnuWin32 then grep
The basic differences are that findstr has some regular expressions support. Grep supports regular expressions best.
C:\>dir | find "abc"
C:\>dir | find /i "abc"
find /?
and findstr /?
shows you what the switches do.
Gnuwin32 has "packages". If you download GnuWin32, I suggest the coreutils package for a bunch of basic useful utilities you'd be familiar with, but grep isn't in that one it's its own package.
Added
GnuWin32's grep, last time I checked, is old. Cygwin's grep is far more up to date. Also bear in mind that many people use Virtual Machines rather than windows ports of *nix commands.
9
If you would rather use grep
, rather than findstr
, there is a single .exe
file version in UnxUtils, so it's portable and there is no need to install it, or use something like Cygwin.
3@userunknown well, as an example, the unxutils sed command is older and doesn't support \d e.g. \d22 to specify quote. An older wget might not download a page that a newer one will, and if you ask for support they may ask that you get the latest one as that can solve some issues. A really old grep no doubt won't have the level of regex support that a current one has – barlop – 2014-09-03T18:59:47.853
This is interesting but would require installing that tool. – chrisjlee – 2011-06-22T21:11:23.947
3@Chris: There is no installation. It's just a .zip
file of .exe
files. The only small downside is that the versions are slightly old. – paradroid – 2011-06-22T21:13:43.960
6What's the downside of being old? Can software be old? All fresh bytes. – user unknown – 2011-06-23T01:43:11.423
3
GnuWin32 also provides grep
for Windows: http://gnuwin32.sourceforge.net/packages/grep.htm
3
You can try installing Chocolatey on Windows, and through that, install the Gow tool. This will provide you with grep
on Windows.
Gow
stand for GNU on Windows. It provides Unix command line utilities on Windows.
1It's a package manager , like an apt-get but for windows. It got its name 'cos the developer based it off of NuGet which is a package manager for visual studio, and that sounds like nugget (as in chicken nugget), so he thought he'd be "funny" or "clever" and call his program Chocolatey NuGet . I heard that on a podcast that interviewed him. Gow can be downloaded independently too – barlop – 2015-07-29T00:01:57.827
1I am new to Gow, after few days of usage I can testify that I like it a lot - it is better than all the older ports of UNIX utils that I had used before. – Radim Cernej – 2016-01-24T20:23:48.720
@RadimCernej your comment isn't useful unless you state how you think it is better. – barlop – 2016-07-18T15:57:57.660
@barlop: Since I wrote my Jan-2016 comment I gradually and completely switched to Gow. So far no bugs, it has all the utilities that I need/use, path are output in Microsoft format (making it easy to copy/paste to other programs). Gow is non-invasive enough (easy to install and remove) that I strongly recommend that you test-drive it. – Radim Cernej – 2016-07-18T19:35:49.393
2
Bash use
$ ls | grep root
Cmd use
> dir /b | findstr root
where /b
stands for bare list of directories and files
2This duplicates another answer and adds no new content. Please don't post an answer unless you actually have something new to contribute. – DavidPostill – 2016-05-02T10:04:23.543
@DavidPostill: This is fast solution for "TL;DR" users (like me) – vladkras – 2016-05-04T12:17:15.803
That is no excuse for adding an answer that doesn't contribute anything new. In any case the accepted answer will always appear first so it will be read before yours. – DavidPostill – 2016-05-04T12:18:33.113
@DavidPostill, don't mind, but accepted answer contain multiple excessive and unexplained keys /B
, /R
and /C
OP didn't ask about, while my answer gives exact equivalent – vladkras – 2016-05-04T12:37:51.717
The /b
is to make the dir
output the same as the ls
output. If you test it you will see the difference. So it is not the exact equivalent. – DavidPostill – 2016-05-04T12:43:42.250
2
I wrote a Windows alternative to grep using Hybrid Batch/JScript code. I wrote this because getting the escape characters right in the GNU Win32 grep port was a real pain. This version works much more like how you would want the GNU version to work in Windows:
@set @junk=1 /*
@cscript //nologo //E:jscript %~f0 %*
@goto :eof */
var args=WScript.Arguments, argCnt=args.Length, stdin=WScript.StdIn, stdout=WScript.StdOut;
var replaceSingleQuotes=false, printMatchesOnly=false, matchString, flagString, regex, argDx=0;
if(argCnt==0) {
throw new Error("You must provide search criteria.");
}
flagString=""
if(argCnt>1) {
for(var bLoop=true; bLoop&&argDx<argCnt-1; argDx++) {
switch(args(argDx)) {
case '-t': replaceSingleQuotes=true; break;
case '-o': printMatchesOnly=true; break;
case '-g': flagString+="g"; break;
case '-i': flagString+="i"; break;
case '-m': flagString+="m"; break;
default: bLoop=false; break;
}
}
}
if(replaceSingleQuotes) {
matchString=args(argCnt-1).replace("'", '"');
} else {
matchString=args(argCnt-1);
}
if(printMatchesOnly) {
while(!stdin.AtEndOfStream) {
var sLine=stdin.ReadLine();
if(flagString.Length) regex=new RegExp(matchString, flagString);
else regex=new RegExp(matchString);
var m,matches=[],startDx=0;
while((m=regex.exec(sLine.substr(startDx))) !== null) {
stdout.WriteLine(m[0]);
startDx+=m.lastIndex;
}
}
} else {
if(flagString.Length) regex=new RegExp(matchString, flagString);
else regex=new RegExp(matchString);
while(!stdin.AtEndOfStream) {
var sLine=stdin.ReadLine();
if(regex.test(sLine)) {
stdout.WriteLine(sLine);
}
}
}
You can always find the latest version on my Gist page for this.
1
Multi replacer program has been prepared so that many functions can be carried out by using command line parameters. Command line usage is seen below:
MultiReplacer [Multi Replacer File] | [Search files] | [Search folders]
[-Subs] [-NoSubs] [-IncPtr=pattern] [-ExcPtr=patterns] [-DestDir=destination]
[-DMAnyTime]
[-DMWithinanhour] [-DMToday] [-DMYesterday] [-DMThisweek] [-DMThismonth]
[-DMThisYear]
[-CDMAfter=date] [-CDMBefore=date] [-MinFileSize=bytes count]
[-MaxFileSize=bytes count]
[-Search=text] [-Case] [-NoCase] [-Regex] [-NoRegex] [-SubMatchText=text]
[-ReplaceText=text]
[-StartSearch] [-StartReplace] [-AutoClose] [-StopAfterMatchThisFile] [-StopAfterMatchAll]
[-ExtractedWordsFile=filename] [-ExtractedLinesFile=filename] [-
ReportFile=filename]
0
I would suggest using busybox-w32, since it is only about 500 KB in size and actively maintained.
So that in your case, in the command prompt, it is:
busybox ls | busybox grep root
You can use doskey
in a command prompt launch by a batch file to make a command, like:
doskey ls="path\to\busybox.exe" ls $*
doskey grep="path\to\busybox.exe" grep $*
Then you can use ls | grep root
on the command prompt.
it has an android build too – barlop – 2016-07-18T15:59:07.613
0
If you want to add the simplest grep
to your windows environment, then navigate to c:\windows\system32
and add a little batch script by using this command:
echo findstr %1 > grep.bat
Now you can
dir | grep notepad.exe
which is really a scary mix of shit. So add another batch script for ls
as explained in this post
echo dir %1 > %systemroot%\system32\ls.bat
Now things look a bit familiar
ls | grep notepad
HTH
0
You can still use your familiar grep
and other Linux commands by downloading this tool UnxUtils and add it location to your PATH
environment variable
unxutils grep isn't as late as you can get.. unx grep is 2.4.2
but gnuwin32 has a later one 2.5.4
– barlop – 2015-07-29T00:05:18.443
-2
echo findstr %1 %2 %3 %4 %5 > %systemroot%\grep.cmd
That's gonna be quick and dirty equivalent.
C:\Windows\system32>dir | grep xwiz
C:\Windows\system32>findstr xwiz
2009.06.10 23:03 4.041 xwizard.dtd
2009.07.14 03:39 42.496 xwizard.exe
2009.07.14 03:41 432.640 xwizards.dll
By the way, this dull answer has 44 upvotes on famous russian dev blog https://habrahabr.ru/post/71568/
– vladkras – 2016-05-02T05:53:02.777
9don't confuse ms-dos with the command prompt. + use of find and findstr and even grep, for windows.. is surely easily found with a quick search. there are even tables of equivalent commands in linux and windows. also you bash example is not even specific to bash.. just I suppose, *nix OS. – barlop – 2011-06-22T21:32:03.047