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

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

假设我想复制一个目录的内容,不包括名字包含单词‘Music’的文件和文件夹。

cp [exclude-matches] *Music* /target_directory

应该用什么代替[排除-匹配]来完成这一任务?


小唯快跑啊
浏览 726回答 3
3回答

守着一只汪

在Bash中,您可以通过启用extglob选项,如下(替换ls带着cp当然,添加目标目录)~/foobar> shopt extglobextglob        off~/foobar> lsabar  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

翻过高山走不出你

(据我所知)不是在bash,而是:cp `ls | grep -v Music` /target_directory我知道这不是你想要的,但它会解决你的例子。
打开App,查看更多内容
随时随地看视频慕课网APP