bash - 在 unix/linux shell 中进行模式匹配时如何使用反向或负通配符?

标签 bash shell pattern-matching glob

假设我想复制一个目录的内容,不包括名称中包含“音乐”一词的文件和文件夹。

cp [exclude-matches] *Music* /target_directory

应该用什么代替 [exclude-matches] 来实现这一点?

最佳答案

在 Bash 中,您可以通过启用 extglob 选项来实现,就像这样(将 ls 替换为 cp 并添加目标目录,当然)

~/foobar> shopt extglob
extglob        off
~/foobar> ls
abar  afoo  bbar  bfoo
~/foobar> ls !(b*)
-bash: !: event not found
~/foobar> shopt -s extglob  # Enables extglob
~/foobar> ls !(b*)
abar  afoo
~/foobar> ls !(a*)
bbar  bfoo
~/foobar> ls !(*foo)
abar  bbar

您稍后可以禁用 extglob

shopt -u extglob

关于bash - 在 unix/linux shell 中进行模式匹配时如何使用反向或负通配符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/216995/

相关文章:

linux - 替换文本文件中的模式

bash - Dockerfile中RUN CD和WORKDIR之间的区别

python - Scrapy Shell - 如何更改 USER_AGENT

python - 如何在其默认程序中启动文件,然后在脚本完成时关闭它?

java - 不能使用/(斜杠)执行 matcher.replaceAll(...)

shell - 如何测试 Bash 中是否存在进程?

bash - 编写脚本检查远程主机服务是否运行

java - 模式 Java - 正则表达式

scala - 简化大规模匹配案例 - Scala

bash - 在 msys bash 上包含换行符时 PS1 命令替换失败