如何在zsh中指定与file.txt匹配但不匹配file.x.txt的glob?

时间:2014-01-28 20:27:06

标签: zsh glob

假设我有一堆文件,其中一个文件有.txt作为扩展名,其他文件有.x.txt ,其中x可以是。如何提取仅具有.txt扩展名的文件?

这是一个可重复的例子:

touch file.txt file.x.txt
echo *.txt
# file.txt file.x.txt

1 个答案:

答案 0 :(得分:2)

% touch file.txt file.x.txt
% echo [^.]#.txt 
file.txt

来自FILENAME GENERATION的{​​{1}}部分:

man zshexpn

因此,这匹配任何以 [...] Matches any of the enclosed characters. Ranges of characters can be specified by separating two characters by a `-'. A `-' or `]' may be matched by including it as the first character in the list. There are also several named classes of characters, in the form `[:name:]' with the following meanings. The first set use the macros provided by the operating system to test for the given character combinations, including any modifications due to local language settings, see ctype(3): ... [^...] [!...] Like [...], except that it matches any character which is not in the given set. ... x# (Requires EXTENDED_GLOB to be set.) Matches zero or more occurrences of the pattern x. This operator has high precedence; `12#' is equivalent to `1(2#)', rather than `(12)#'. It is an error for an unquoted `#' to follow something which cannot be repeated; this includes an empty string, a pattern already followed by `##', or parentheses when part of a KSH_GLOB pattern (for example, `!(foo)#' is invalid and must be replaced by `*(!(foo))'). 结尾且不包含其他.txt个字符的字符串。