Answers:
该extended_glob选项提供zsh自己的扩展glob语法。
setopt extended_glob
rm -- ^*.dmg
rm -- ^*.(dmg|txt)
您可以设置ksh_glob获取ksh globs的选项。请注意,在通常情况下,否定模式是单词中的最后一句话,zsh可能会将括号解析为glob限定符(在ksh仿真模式下不会这样做)。
setopt ksh_glob
rm -- !(*.dmg|*.txt)
setopt no_bare_glob_qual
rm -- !(*.dmg)
ksh_glob选项。“未找到事件:目录”表示您正在运行bash(它也了解!(…)语法,但仅在之后shopt -s extglob)。
setopt ksh_glob; echo !(a|b)虽然setopt ksh_glob; echo !(a)没有工作(“预期数量”)...
您可以使用find而不是shell:
find . -mindepth 1 -maxdepth 1 ! -name "*.dmg" -delete
来自man find:
! expr True if expr is false. This character will also usually need
protection from interpretation by the shell.
-name pattern
Base of file name (the path with the leading directories removed)
matches shell pattern pattern.
-delete
Delete files; true if removal succeeded. If the removal failed,
an error message is issued. If -delete fails, find's exit status
will be nonzero (when it eventually exits). Use of -delete
automatically turns on the -depth option.
如果您find出于某种原因无法使用,可以使用以下方法zsh(或其他外壳)使用。zsh作为zsh,可能有一种更简单的方法来执行此操作,但是由于我是一个bash男人,所以我想到了:
for file in *; do if [[ ! "$file" == *.dmg ]]; then rm $file; fi; done
find [Process completed]
find。我建议您find尽管发布有关您的问题的问题,但这不应该发生。如果这样做,请包含的输出type -a find。
rm !(*.dmg)在之后shopt -s extglob。
find is a shell function find is /usr/bin/find
/usr/bin/find。您有一个find在bash配置文件中定义的函数。由于您使用的是OSX,因此可能是~/.profile。
删除文件的另一种方式是find,xargs和rm:
find . -mindepth 1 -maxdepth 1 ! -name '*.dmg' -print0 | xargs -0 rm
rm -r secrets/!(directory)它总是在询问number expected或者有时它给了我event not found: directory