在 unix/linux shell 中进行模式匹配时如何使用反向通配符或负通配符? [英] How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?

查看:32
本文介绍了在 unix/linux shell 中进行模式匹配时如何使用反向通配符或负通配符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我要复制目录的内容,但不包括名称中包含音乐"一词的文件和文件夹.

Say I want to copy the contents of a directory excluding files and folders whose names contain the word 'Music'.

cp [exclude-matches] *Music* /target_directory

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

What should go in place of [exclude-matches] to accomplish this?

推荐答案

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

In Bash you can do it by enabling the extglob option, like this (replace ls with cp and add the target directory, of course)

~/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

您可以稍后使用

shopt -u extglob

这篇关于在 unix/linux shell 中进行模式匹配时如何使用反向通配符或负通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆