Что означают звездочка и точка в find -name «* .mp3»?

В find -name "* .mp3" есть звездочка перед .mp3. Разве * не соответствует предшествующему символу любое количество раз? Но перед звездочкой ничего нет, так что же * ? Или это потому, что команда find не использует регулярные выражения? Итак, это . в .mp3 символ регулярного выражения или находит просто интерпретирует его как точку для сопоставления?

16
задан 16 June 2020 в 06:58

3 ответа

The -name predicate takes a glob, not a regular expression.

In a regular expression, * would match zero or more of the preceding item. But since this is a glob, it matches zero or more of any character. That is, * in a glob means the same thing as .* in a regular expression.

In a regular expression, . would match exactly one of any character. But since this is a glob, it is not treated specially, and simply matches a literal . character. That is, . in a glob means the same thing as \. or [.] in a regular expression.

If you did want to match a single occurrence of any character in a glob, you would use ?. In a glob, ? means the same thing as . in a regular expression. This is quite different from ? in a regular expression, which matches zero or one of the preceding item (i.e., makes the preceding item optional).

The only syntax that's mostly the same in both globs and regular expressions is character classes enclosed in [ ]. For example, [aeiou] matches any character that is either a, e, i, o, or u. If you write a [ ]-delimited character class that works in most regex dialects, it's likely to be a character class with the same meaning when used in a glob, too.


Other than as the operand to find's -name and -iname predicates, another place where globs are widely used is by your shell. Unquoted globbing metacharacters are expanded by your shell automatically into a list of separate arguments corresponding to the filenames they match.

This is why it's important to quote such a pattern when passing it to find. If you don't, your shell will try to expand it. If your shell does expand it, find won't see the pattern itself, but instead the result of its expansion. This is undesirable and often confusing.

Note that your shell (probably bash, but this is true of Unix-style shells in general) will not, as its default behavior, expand ? or * to match a leading dot in a filename. For example, unless you've told it to behave otherwise, your shell expands ? to the filenames in the current directory that are one character long, other than . , even though a . entry is always present. And it expands * to the filenames in the current directory that don't start with .. (In bash, shopt -s dotglob would change this for the shell instance you run it in, and shopt -u dotglob would restore the default behavior.) It is only leading dots that are special, not other dots.

This differs from the behavior of find's -name and -iname predicates, where ? and * are always permitted to match a leading ..

39
ответ дан 19 June 2020 в 21:24

Звездочка (обычно известная как «Оператор подстановочного знака») говорит, что find должен вернуть все файлы с расширением .mp3 .

1
ответ дан 19 June 2020 в 21:24

Поскольку вы используете двойные кавычки, bash пытается раскрыть "*. Mp3 " glob.

Если в текущем каталоге есть несколько файлов *. Mp3 , их имена заменят глобуса.

Чтобы продемонстрировать, поместите echo в начале строки, например echo find ... .

Если вы используете одинарные кавычки ( '), расширение глобуса не выполняется.

Используйте « '*. Mp3' » вместо «» *. Mp3 «».

-8
ответ дан 19 June 2020 в 21:24

Другие вопросы по тегам:

Похожие вопросы: