How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?

前端 未结 11 1592
梦如初夏
梦如初夏 2020-11-22 13:50

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_direc         


        
11条回答
  •  北海茫月
    2020-11-22 14:32

    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
    

    You can later disable extglob with

    shopt -u extglob
    

提交回复
热议问题