Equivalent of Unix find command on Windows

38

6

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]

ARV

Posted 2012-03-16T15:10:58.107

Reputation: 585

2Is something wrong with my answer? Can I improve it? Do you still need help? – JohannesM – 2012-06-22T16:55:07.330

Sorry, I had lost sight of this thread a while ago. Thanks for your answer and apologies for not accepting it sooner. – ARV – 2012-06-25T09:44:46.180

Answers

23

dir <drive: [drive:]> /s | findstr /i <pattern>

- alternative -

dir /s <drive:>\<pattern>

example

dir c: d: /s | findstr /i example.txt

- alternative -

dir /s c:\example.txt

JohannesM

Posted 2012-03-16T15:10:58.107

Reputation: 822

1for the most similar results I use \b for brief (output only paths); find <folder> -name <pattern> -> dir /s /b <folder><pattern>. E.g. find /tmp -name *.txt -> dir \s \b C:\temp\*.txt. However dir always returns a list of absolute paths, whereas find always gives paths prefixed with <folder> – Hashbrown – 2015-05-26T03:51:13.900

dir c: /s example.txt works, too. – DevSolar – 2012-06-25T11:55:32.200

@DevSolar can you recheck your command? I've tested it on Windows 5.1 Build 2600 SP3, and your command just gives me the list of files in the directory c: – JohannesM – 2012-06-25T12:07:02.657

1Uh... sorry. Serves me right to type from memory. dir /s C:\example.txt it is. – DevSolar – 2012-06-25T12:14:14.373

28

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

  1. Find-ChildItem -Type f -Name ".*.exe"
  2. Find-ChildItem -Type f -Name "\.c$" -Exec "Get-Content {} | Measure-Object -Line -Character -Word"
  3. Find-ChildItem -Type f -Empty
  4. Find-ChildItem -Type f -Empty -OutObject
  5. Find-ChildItem -Type f -Empty -Delete
  6. Find-ChildItem -Type f -Size +9M -Delete
  7. Find-ChildItem -Type d
  8. Find-ChildItem -Type f -Size +50m -WTime +5 -MaxDepth 1 -Delete

Disclosure: I am the developer of Find-ChildItem cmdlet

Jagadish G

Posted 2012-03-16T15:10:58.107

Reputation: 389

2Thank you. This is definitely more in mind of what I'd be looking for in answering this question. – supercheetah – 2015-02-02T16:37:44.827

12Find-ChildItem is not an official cmdlet and it is not included in PowerShell; you have to download this cmdlet from some guy's OneDrive. There's no difference between that and just downloading bash, cygwin, unixutils or any other program that just lets you run UNIX's find. – walen – 2018-04-18T13:49:47.973

23

With no additional cmdlets installed, you can simply use Get-ChildItem:

Get-ChildItem -Filter *.zip -Recurse $pwd

djhaskin987

Posted 2012-03-16T15:10:58.107

Reputation: 349

1In which case you probably want to use one of the short aliases dir, ls or gci, unless you are writing a script. – Swonkie – 2018-08-31T07:14:37.080

5

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.

Joshua Kan

Posted 2012-03-16T15:10:58.107

Reputation: 111

2

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 .

Bill Moore

Posted 2012-03-16T15:10:58.107

Reputation: 191

Find -exec grep {} from UnixUtils doesn't work properly it seems "no such file or directory". This solution: PowerShell.exe -Command "dir -Recurse -ea 0 | ? FullName -like '*.log' | sls error", from within a batch script works. Note: must use single quotes inside, double quotes outside. – kevinf – 2018-11-06T16:18:57.903