Gnuwin32 find.exe expands wildcard before performing search [closed]
Asked Answered
B

5

20

I am using Gnuwin32 binaries on a Windows environment.
When I want to find files of a certain type, let's say PDF, I usually run:

find . -iname '*.pdf' -print

This works perfectly on any UNIX system.

find.exe . -iname "*.pdf" -print

But under Windows, having replaced single quotes with double-quotes, it only works when there is no pdf file in the current directory, otherwise the * gets expanded.

Worse: when there is exactly one PDF file in the current directory, it will expand, there will be no syntax error and you will get wrong results.

I have tried escaping the * with a caret, a backslash, a star itself, putting inside double quotes: nothing works for me.

Real example:

Okay, here are all my files:

C:\tmp>find . -type f
./a/1.pdf
./a/2.pdf
./a/aa/1.pdf
./b/1.pdf
./b/bb/1.pdf
./b/bb/2.pdf

Good behaviour, wildcard was not expanded

C:\tmp>find . -iname "*.pdf"
./a/1.pdf
./a/2.pdf
./a/aa/1.pdf
./b/1.pdf
./b/bb/1.pdf
./b/bb/2.pdf

C:\tmp>cd a

Caution, inconsistent behaviour, wildcard was expanded:

C:\tmp\a>find . -iname "*.pdf"
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

C:tmp\a>cd ..\b

Caution, inconsistent behaviour, wildcard was expanded :

C:\tmp\b>find . -iname "*.pdf"
./1.pdf
./bb/1.pdf

Thank you

Barometrograph answered 22/10, 2010 at 9:26 Comment(8)
I don't understand what you want. Why don't you want * to get expanded? If not, how do you think find shows you the results?Nutation
Because I want find to have argv[3] equal to {'*','.','p','d','f'}. Find is adult enough to interpret the jokers.Barometrograph
Example: I have ./a.pdf, ./b/a.pdf, ./b/b.pdf ; I run find . -iname "*.pdf". Cmd expands it to find . -iname a.pdf, and eventually I do not get ./b/b.pdf in my results. Of course with a Unix shell, find . -iname '*.pdf' gets me all pdf files.Barometrograph
show your batch code in your question to better understand your situationNutation
I run find . -iname "*.pdf" and i don't have problems. It will show me all my pdf files...Nutation
do you have one or more pdf files in your current directory?Barometrograph
@ghostdog74, I have included real examples for you to be able to figure it out.Barometrograph
ezwinport's find (4.2.30) is also working as expected, more up to date than UnxUtils perhaps....Dizen
B
18

I have found myself the solution to my problem.

  • Gnuwin32's find.exe is not working on recent Windows Versions (Vista, Seven) because it expands wildcards matching only the contents of the current directory.
  • Similarly, an old version of find.exe from UnxUtils suffered the same bug.
  • The latest find.exe from UnxUtils is working.
Barometrograph answered 22/10, 2010 at 11:31 Comment(1)
I got caught by this myself and answered another post (#33860641) with the answer. For mingw, set a global "int _CRT_glob = 0;" in the same source file as main or Winmain. Globbing is then turned off. It's quite a shock for an experienced programmer the first time you see this happening!Ascensive
C
18

One workaround is to add a wildcard/expansion that the Windows shell does not expand, but GNU find does:

find.exe . -name *[.:]pdf -print

The Windows shell[*] does not interpret/expand square braces. In addition, colon is not a valid character in Windows filenames, so this pattern cannot match any Windows filename, and the Windows shell will always pass the pattern through to find.exe.

Find.exe will then find any files ending in .pdf or :pdf , but since no files can have a name ending in :pdf under Windows, it will only find files ending in .pdf.

[*] It's actually the C runtime that does/not perform these wildcard expansions. I don't understand the Win32 C runtime well enough to refine the distinction, so for now for the purpose of this workaround, I'm just saying 'shell'.

Calefactory answered 24/7, 2013 at 20:36 Comment(1)
small nitpick: it's the C runtime which performs the expansion, not the shell.Cerous
S
3

I suffered this problem this afternoon. Benoit's UnxUtils can work. I also find MinGW's find.exe can work,it is under my

"MinGW\msys\1.0\bin"

directory. And it is consistent with the manual.

gnuwin32 and UnxUtils: find.exe . -name GameCli* work, but find.exe . -name 'GameCli*' doesn't work.

MinGW's find.exe . -name 'GameCli*' work.

Subdiaconate answered 21/5, 2012 at 9:50 Comment(0)
I
2

I haven't found anything better than just avoiding wildcard characters

find.exe . -iregex ".+\.pdf" -print
Ivonne answered 5/6, 2012 at 5:30 Comment(1)
Note: -iregex doesn't work on some versions of gnuwin32 find.exe due to a bugCalefactory
N
0

@OP, i have consistent behaviour

C:\test\temp>find . -iname "*.txt"
./1.txt
./2.txt

C:\test\temp>cd a

C:\test\temp\a>find . -iname "*.txt"

C:\test\temp\a>cd ..\b

C:\test\temp\b>find . -iname "*.txt"

C:\test\temp\b>find --version
GNU find version 4.2.20
Features enabled: CACHE_IDS D_TYPE

You may want to try to use findutils instead of UnxUtils.

Nutation answered 22/10, 2010 at 9:57 Comment(3)
Same. When I use C:\gnuwin32\bin\echo.exe "*.txt" it outputs a.txt b.txt. Maybe it is a cmd.exe setting?Barometrograph
ghostdog74: thee this message. What version of Windows are you using?Barometrograph
I have found a solution for my problem, thank you for helping me diagnose it. Actually, it did not come from cmd.exe which never expands a * and lets programs do that themselves, but from find.exe which behaves differently on recent versions of Windows.Barometrograph

© 2022 - 2024 — McMap. All rights reserved.