Why I'm I getting the no matches found error when there is a match?

7,927

Solution 1

The * is expanded by the shell before sudo is invoked. If you don't have access to that directory, the zsh shell will complain with "no matches found". If the NOMATCH shell option is unset in the zsh shell, the shell would have left the pattern unexpanded and ls would instead generate a "no such file or directory" error (unless there was something with the literal name * in that directory). With NOMATCH set, which it is by default, sudo ls would not even be invoked.

You may do this instead:

sudo sh -c 'ls -l /var/solr/data/new_core/_default/*'

This prevents the current shell from expanding the * and instead invokes sh with the command line that you want to execute as root.

Solution 2

In first case :

  1. access right prevent shell(*) expansion of /var/solr/data/new_core/_default/* when your are centos.

  2. command expand then as /var/solr/data/new_core/_default/* in first case.

  3. there is no * file or dir in /var/solr/data/new_core/_default/, hence the no match found.

In second case, ls find file and list them.

(*) this is the Filename Expansion from zsh expansion (see man zshexpn).

FILENAME GENERATION

If a word contains an unquoted instance of one of the characters `*', ...

The word is replaced with a list of sorted filenames that match the pattern.

as you are centos before sudo zsh could not access dir (e.g. file conf ), hence the no match found.

Share:
7,927

Related videos on Youtube

ans
Author by

ans

Updated on September 18, 2022

Comments

  • ans
    ans over 1 year
    centos@ip-10-0-5-4 ~ $ sudo ls -l /var/solr/data/new_core/_default/*
    zsh: no matches found: /var/solr/data/new_core/_default/*
    centos@ip-10-0-5-4 ~ $ sudo ls -l /var/solr/data/new_core/_default/ 
    total 4
    drwxr-xr-x. 3 root root 4096 Mar 28 07:34 conf
    
  • Stéphane Chazelas
    Stéphane Chazelas about 6 years
    no, same as as @Kusalananda, the error is output by zsh here, not ls. sudo and ls are not run here. They would be with that one * file if using bash without the failglob option or most other Bourne-like shells (a behaviour that can be considered as a bug).
  • Stéphane Chazelas
    Stéphane Chazelas about 6 years
    Note that if there was a file called * in that directory, with Bourne/ksh/bash (without failglob), you wouldn't get an error and you would be mislead into thinking that it's the only file there. See also Why is nullglob not default?