12

Is there an equivalent of the GNU locate command in Windows 7/8?

locate can take as input a file name and gives as output all the paths where files named similarly to input are, e.g.:

locate file-with-long-name.txt
/var/www/file-with-long-name.txt
Indrek
  • 24,204
  • 14
  • 90
  • 93
Razor
  • 1,340
  • 4
  • 16
  • 29

5 Answers5

9

No, there is not a Windows cmd or PowerShell builtin equivalent to Linux/GNU's locate command. However, functional equivalents include cmd.exe's dir /s as described by JKarthik, and these PowerShell options:

PS> Get-ChildItem -Recurse . file-with-long-name.txt

Note the use of ., telling PowerShell where to begin the search from. You can, of course, shorten when typing at the command line:

PS> gci -r . file-with-long-name.txt

I do this a lot, so I added a function to my profile:

PS> function gcir { Get-ChildItem -Recurse . @args }
PS> gcir file-with-long-name.txt

This allows wildcards, similar to locate:

PS> gcir [a-z]ooo*.txt

See help about_Wildcards for more details. That can also be written with Where-Object like this:

PS> gcir | where { $_ -like "[a-z]ooo*.txt"}

locate has an option to match with regexes. So does PowerShell:

PS> gcir | where { $_ -match "A.*B" }

PowerShell supports full .NET Regular Expressions. See about_Regular_Expressions.

You can do other types of queries, too:

PS> gcir | where { $_.Length -gt 50M }  # find files over 50MB in size

Performance of these approaches is slow for large collections of files, as it just searches the filesystem. GNU locate uses a database. Windows now has a searchable database, called Windows Desktop Search. There is an API to WDS, which someone has wrapped with a PowerShell cmdlet, here: http://www.codeproject.com/Articles/14602/Windows-Desktop-Search-Powershell-Cmdlet, allowing things like:

PS> get-wds “kind:pics datetaken:this month cameramake:pentax” 

with much better performance than Get-ChildItem, and this kind of rich query (and awkward syntax). Also, note that curly quotes work fine in PowerShell, so no need to edit that sample when copy/pasting it.

Maybe someone will find (or write) PowerShell cmdlets that allow idiomatic queries to WDS.

Jay Bazuzi
  • 4,160
  • 6
  • 32
  • 41
3

For a PowerShell solution, try this:

Get-ChildItem -Filter "file-with-long-name.txt" -Recurse

This returns all files that match the given name in the current directory and its subdirectories.

The -Filter parameter accepts wildcards. If the current directory contains system files that you don't have access to, add -ErrorAction SilentlyContinue to suppress errors.

For more information, see Get-Help Get-ChildItem.

Indrek
  • 24,204
  • 14
  • 90
  • 93
  • 1
    At the command prompt, I abbreviate `-ErrorAction SilentlyContinue` to `-ea 0`, for ease of typing. – Jay Bazuzi Nov 07 '12 at 14:59
  • This answer is better prepended with a warning about the huge difference between `locate` (which looks into a previously generated database, with the pro of speed and the con of not being instantaneously up-to-date) and `Get-ChildItem`. – sancho.s ReinstateMonicaCellio Dec 20 '13 at 17:02
2

You can use the following command on windows shell:

dir [filename] /s

Where filename is the name of the file you're looking for, and /s refers to include sub-directories in the search.

Update The following command with /B shows only bare format, exactly as required. And this seems to be a tad faster.

Do try:

 dir [filename] /s /B

Source: Windows 8 Command Line List and Reference

Indrek
  • 24,204
  • 14
  • 90
  • 93
tempidope
  • 395
  • 1
  • 5
  • 16
  • this is *scarily slow* compared to linux, but it does eventually find the file. Will mark it as answer if nobody else finds a faster alternative – Razor Nov 07 '12 at 14:00
  • 1
    Hey, /B displays in bare format (only the location of the file) and this seems to be tad faster. Do check. – tempidope Nov 07 '12 at 15:48
  • The difference is that `locale` looks in a database (generate/updated by `updatedb`) and `dir` actually searches the full hard disk. – Joachim Sauer Nov 07 '12 at 16:49
  • 1
    `dir/s` is slow because it brute-forces the search (looks *everywhere*). It harks back to DOS (being a DOS command), which is why it operates that way. I suspect the only reason it still exists is as a fall-back in case your index database isn't adequate. – Ben Richards Nov 07 '12 at 17:50
  • This answer is better prepended with a warning about the huge difference between `locate` (which looks into a previously generated database, with the pro of speed, and the con of not being instantaneously up-to-date) and `Get-ChildItem`. – sancho.s ReinstateMonicaCellio Dec 20 '13 at 17:07
0

You can install Cygwin to mimic the linux toolset or install the MinGW port and you'll have access to a shell with almost all the familiarity of a native linux system

-2

If you are not used to UNIX locate you don't know what you are missing.

locate returns filenames only from every drive in your PC instantly.

Type in:

locate some filename

and every name specified will show up instantly. It will NOT look inside your files for fragments. The speed comes from a database initiated with updatedb which can take time, and is normally a daily cron-job.

I could not find anything useful in this thread so I added my solution: Install Cygwin64, open a terminal, updatedb then locate

I did a test to highlight performance differences, I did a search of putty.exe

On Win10 it took 12 min 50 sec in Explorer, and 4 seconds in Cygwin terminal.

I was not interested in fragments, which was a "bonus" on Explorer search which makes it take more time. Yes, indexing is turned on.

CSV
  • 1
  • 2
    I believe this shows the usefulness of UNIX locate, it doesn't answer the question very well. – fungusakafungus Feb 12 '21 at 21:39
  • Right, I gave up on any MS-Windows answers, as most MS-Windows experts are not familiar with this search utility. Cygwin is a Windows program, albeit clunky for just this function. Windows search is badly broken, as can be seen from the above search time. Don't get me started on the Win10 GUI. – CSV Feb 15 '21 at 13:57
  • `updatedb` has been very fast for me compared to the time it takes for a trivial search in Windows Explorer. Windows has pathetic indexing performance. Windows is pathetic. – Peter Kionga-Kamau Sep 22 '21 at 01:50