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

前端 未结 11 1607
梦如初夏
梦如初夏 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:22

    One solution for this can be found with find.

    $ mkdir foo bar
    $ touch foo/a.txt foo/Music.txt
    $ find foo -type f ! -name '*Music*' -exec cp {} bar \;
    $ ls bar
    a.txt
    

    Find has quite a few options, you can get pretty specific on what you include and exclude.

    Edit: Adam in the comments noted that this is recursive. find options mindepth and maxdepth can be useful in controlling this.

提交回复
热议问题