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

标签 zsh glob

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

这是一个可重现的示例:

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

最佳答案

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

来自 man zshexpnFILENAME GENERATION 部分:

   [...]  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 结尾且不包含其他 . 字符的字符串。

关于zsh - 如何在 zsh 中指定匹配 file.txt 但不匹配 file.x.txt 的 glob?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21416095/

相关文章:

macos - 我收到此错误 "can' t 解压缩数据;在 MacOS 10.15 上安装 AWS EB CLI 时,zlib 不可用

path - 设置 zsh PATH 不产生所需的顺序

Swift ZSH 自动补全

regex - 通过 Vim 的 Regex 和 globbing 查找重复

Python检查目录中具有特定名称的所有文件

python - 镜像序列在OpenCV中不维护

shell - Z shell 中的 cd -1、-2、-3 等

video - 使用 zsh 和 ffmpeg 从嵌套目录中的视频递归生成静止图像

linux - linux cp 中的通配符

python - 将 WindowsPath 转换为字符串