2
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
Kusalananda
  • 333,661
ans
  • 31

2 Answers2

6

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.

Kusalananda
  • 333,661
1

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.

Archemar
  • 31,554
  • 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 Mar 28 '18 at 13:06
  • @StéphaneChazelas edited. Yet, in both case (bash or zsh) the non -expansion of * is cause of problème. – Archemar Mar 28 '18 at 14:14
  • 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? – Stéphane Chazelas Mar 28 '18 at 14:16