How to use 'mv' command to move files except those in a specific directory?

前端 未结 8 1640
夕颜
夕颜 2020-12-23 11:35

I am wondering - how can I move all the files in a directory except those files in a specific directory (as \'mv\' does not have a \'--exclude\' option)?

相关标签:
8条回答
  • 2020-12-23 12:12

    First get the names of files and folders and exclude whichever you want:

    ls --ignore=file1 --ignore==folder1 --ignore==regular-expression1 ...
    

    Then pass filtered names to mv as the first parameter and the second parameter will be the destination:

    mv $(ls --ignore=file1 --ignore==folder1 --ignore==regular-expression1 ...) destination/
    
    0 讨论(0)
  • 2020-12-23 12:13
    #!/bin/bash
    
    touch apple  banana  carrot  dog  cherry
    
    mkdir fruit
    
    F="apple  banana  carrot  dog cherry"
    
    mv ${F/dog/} fruit
    

    # this removes 'dog' from the list F, so it remains in the current directory and not moved to 'fruit'

    0 讨论(0)
提交回复
热议问题