Skip to content

In Linux, Find Command Fails Even With Quoted and Escaped Asterisk #4680

Closed

Description

@bigboy-pdb-spam

Steps to reproduce

PS> touch ab.txt 'a b.txt'

# Question mark character works

PS> find -name 'a?.txt'
ab.txt

PS> find -name 'a??.txt'
a b.txt

# Asterisk appears to be being expanded into file names

PS> find -name 'a*b.txt'
/usr/bin/find: paths must precede expression: ab.txt
Usage: /usr/bin/find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec|time] [path...] [expression]

# Escaping asterisk doesn't work

PS> find -name 'a`*b.txt'
# No output, which indicates the parameters were accepted, but the search produced no results.

Expected behavior

PS> find -name 'a*b.txt'
./ab.txt
./a b.txt

Actual behavior

PS> find -name 'a*b.txt'
/usr/bin/find: paths must precede expression: ab.txt
Usage: /usr/bin/find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec|time] [path...] [expression]

It appears that the command is being converted into: find -name 'a b.txt' 'ab.txt'

Environment data

> $PSVersionTable
Name                           Value                                           
----                           -----                                           
PSVersion                      6.0.0-beta                                      
PSEdition                      Core                                            
GitCommitId                    v6.0.0-beta.6                                   
OS                             Linux 4.4.0-92-generic #115-Ubuntu SMP Thu Au...
Platform                       Unix                                            
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                         
PSRemotingProtocolVersion      2.3                                             
SerializationVersion           1.1.0.1                                         
WSManStackVersion              3.0 

Activity

  1. mklement0 commented on Aug 27, 2017

    @mklement0
    Contributor

    Yes, globbing currently mistakenly also kicks in when the pattern is quoted.
    A simpler example:

    printf '%s\n' '*'  # '*' is expanded despite being quoted; in Bash, a literal '*' would print.

    This was previously reported in #3931, and a fix is pending: #4489

  2. changed the title [-]Quoted Asterisk Causes Find Command to Fail in Bash[/-] [+]Quoted Asterisk Causes Find Command to Fail in Linux[/+] on Aug 27, 2017
  3. bigboy-pdb-spam commented on Aug 27, 2017

    @bigboy-pdb-spam
    Author

    According to that issue, the asterisk can be escaped using the backquote character, which appears to work for that case:

    PS> echo '11:1' | grep '.`*:.'
    11:1

    However, in the examples that were listed for this issue, using the backquote produced no results, but the command should have displayed the two newly created files.

    PS> touch ab.txt 'a b.txt'
    PS> find -name 'a`*b.txt'
    # No output, but should output the file names:
    # ./ab.txt
    # ./a b.txt

    I'll try this example in the next beta and close this issue if the problem is solved.

    EDIT: I've changed the name of this isssue to highlight the problem involving the quoted and escaped asterisk. If there's a better name, please let me know, or you can update the title if you'd like.

  4. changed the title [-]Quoted Asterisk Causes Find Command to Fail in Linux[/-] [+]In Linux, Find Command Fails Even With Quoted and Escaped Asterisk[/+] on Aug 27, 2017
  5. mklement0 commented on Aug 27, 2017

    @mklement0
    Contributor

    '...' in PS encloses literals, and the newly introduced native globbing feature should treat them as such too. Not doing so is a bug.
    (As is the interpretation of globbing patterns inside double-quoted PS strings).

    In short: Using ` inside '...' before wildcard metacharacters is not a general workaround, nor should it be.

    Adding a ` (backtick) before * disrupts the newly introduced PS-side globbing, but the ` character is passed through to the target utility, which is undesired.
    (In PS, backticks only function as escape characters in unquoted or double-quoted strings.)

    PS> printf '%s\n' '`*'
    `*   # ` was retained

    It explains why your find -name 'a`*b.txt' workaround attempt didn't work.

    The ` is also passed through in your grep example - it just happens to work even with the ` present, but that is unrelated to the issue at hand; you get the same behavior in bash:

    # PS
    PS> echo 'a' | grep '.`*'
    a
    
    # Bash
    $ echo 'a' | grep '.`*'  # !! effectively treated as '.*'
    a

    For reasons I don't understand, placing a ` before regex duplication symbols * and ? causes the ` to be ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-Duplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions