What is the equivalent of the Unix find command on Windows?
I see that the find.exe on Windows is more like a grep. I am especially interested in the equivalent of
find . -name [filename]
dir <drive: [drive:]> /s | findstr /i <pattern>
- alternative -
dir /s <drive:>\<pattern>
dir c: d: /s | findstr /i example.txt
- alternative -
dir /s c:\example.txt
With no additional cmdlets installed, you can simply use Get-ChildItem:
Get-ChildItem -Filter *.zip -Recurse $pwd
The Find-ChildItem Cmdlet in Windows Powershell is an equivalent of Unix/Linux find command
http://windows-powershell-scripts.blogspot.in/2009/08/unix-linux-find-equivalent-in.html
Some of Find-ChildItem Options
Find-ChildItem -Type f -Name ".*.exe"Find-ChildItem -Type f -Name "\.c$" -Exec "Get-Content {} | Measure-Object -Line -Character -Word"Find-ChildItem -Type f -EmptyFind-ChildItem -Type f -Empty -OutObjectFind-ChildItem -Type f -Empty -DeleteFind-ChildItem -Type f -Size +9M -DeleteFind-ChildItem -Type dFind-ChildItem -Type f -Size +50m -WTime +5 -MaxDepth 1 -DeleteDisclosure: I am the developer of Find-ChildItem cmdlet
If you are using Unix's find to search for files in a directory hierarchy, then
the Powershell way is to use Get-ChildItem (alias is gci) cmdlet and filter the results with the Where-Object (alias is where) cmdlet.
For example, to find all files (starting from C:\Users\ and recursively) with the word 'essential' in its name, use the following:
PS> gci -Path "C:\Users\" -Recurse | where {$_.Name -like '*essential*'}
The -like option allows you to use wildcards for pattern matching.
This one is not exactly GNU find, but more closely matches the linux command line philisophy under powershell:
PS> dir -recurse -ea 0 | % FullName | sls <grep_string>
Example:
PS> cd C:\
PS> dir -recurse -ea 0 | % FullName | sls "Program" | sls "Microsoft"
PS> dir -recurse -ea 0 | % FullName | sls "Program" | sls "Microsoft" | out-gridview
Note: Everything returned after "| % FullName" is a string, instead of an object.
You can also use the Where Operator, "?", however, its more work, and not much faster:
PS> cd C:\
PS> dir -Recurse -ea 0 | ? FullName -like "*Program*"
| ? FullName -like "*Microsoft*"
| % FullName
| out-gridview
Here's a quick shortcut:
PS> function myfind {dir -recurse -ea 0 | % FullName | sls $args }
PS> cd C:\
PS> myfind "Programs" | sls "Microsoft"
#find all text files recursively from current directory
PS> myfind "\.txt$"
#find all files recursively from current directory
PS> myfind .
In PowerShell you can use Get-ChildItem (aka ls), as noted in other answers.
ls . -Filter *.zip -Recurse
It might also be useful to get full paths of files instead of short names.
(ls -Path . -Filter *.zip -Recurse).FullName
And you can also easily execute arbitrary commands on the files found.
(ls -Path . -Filter *.zip -Recurse).FullName | ForEach-Object -Process {
# The $_ variable is the path to a located file.
echo "Found file: $_"
}
ls c:\ file.ext -r
You can use this simple powershell command. use -ErrorAction Ignore to get rid of permission errors.
While not a full substitute, this simple batch file solved most of the problem for me:
# findw.bat
#
# usage: findw dir search-pattern
#
dir %1 /s /b | findstr /i %2
On Windows you can use Windows Subsystem for Linux (WSL), install a Linux VM like Debian or Ubuntu. Then you have the C-disk available under /mnt/c.
find /mnt/c/ -name [filename]
You can of course cd into the directory first.
cd /mnt/c/Users/John/Documents
find . -name [filename]
If I try this last search, I need quotes around the filename, while not when using the absolute path.
You can use get-childitem very similar to find.
get-childitem -recurse [startpath] -name [filetofind]
[startpath] is the path where recursion should begin (e.g. . for the current directory)
[filetofind] is what you are looking for.
It is even possible to do this from cmd (without interactieve powershell):
powershell -command "get-childitem -recurse [startpath] -name [filetofind]"