为了账号安全,请及时绑定邮箱和手机立即绑定

在Unix/Linuxshell中模式匹配时,如何使用逆通配符或负通配符?

在Unix/Linuxshell中模式匹配时,如何使用逆通配符或负通配符?

小唯快跑啊 2019-07-26 15:14:13
在Unix/Linuxshell中模式匹配时,如何使用逆通配符或负通配符?假设我想复制一个目录的内容,不包括名字包含单词‘Music’的文件和文件夹。cp [exclude-matches] *Music* /target_directory应该用什么代替[排除-匹配]来完成这一任务?
查看完整描述

3 回答

?
守着一只汪

TA贡献1872条经验 获得超3个赞

在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

之后,您可以用


shopt -u extglob




查看完整回答
反对 回复 2019-07-27
?
翻过高山走不出你

TA贡献1875条经验 获得超3个赞

(据我所知)不是在bash,而是:

cp `ls | grep -v Music` /target_directory

我知道这不是你想要的,但它会解决你的例子。


查看完整回答
反对 回复 2019-07-27
  • 3 回答
  • 0 关注
  • 721 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信