Why I'm I getting the no matches found error when there is a match?
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 :
access right prevent shell(*) expansion of
/var/solr/data/new_core/_default/*
when your arecentos
.command expand then as/var/solr/data/new_core/_default/*
in first case.there is no*
file or dir in/var/solr/data/new_core/_default/
, hence theno 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
.
Related videos on Youtube
ans
Updated on September 18, 2022Comments
-
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 about 6 yearsno, same as as @Kusalananda, the error is output by zsh here, not
ls
.sudo
andls
are not run here. They would be with that one*
file if usingbash
without thefailglob
option or most other Bourne-like shells (a behaviour that can be considered as a bug). -
Stéphane Chazelas about 6 yearsNote that if there was a file called
*
in that directory, with Bourne/ksh/bash
(withoutfailglob
), 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?