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

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

    A trick I haven't seen on here yet that doesn't use extglob, find, or grep is to treat two file lists as sets and "diff" them using comm:

    comm -23 <(ls) <(ls *Music*)
    

    comm is preferable over diff because it doesn't have extra cruft.

    This returns all elements of set 1, ls, that are not also in set 2, ls *Music*. This requires both sets to be in sorted order to work properly. No problem for ls and glob expansion, but if you're using something like find, be sure to invoke sort.

    comm -23 <(find . | sort) <(find . | grep -i '.jpg' | sort)
    

    Potentially useful.

提交回复
热议问题