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

前端 未结 8 1639
夕颜
夕颜 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 11:47

    This isn't exactly what you asked for, but it might do the job:

    mv the-folder-you-want-to-exclude somewhere-outside-of-the-main-tree
    mv the-tree where-you-want-it
    mv the-excluded-folder original-location
    

    (Essentially, move the excluded folder out of the larger tree to be moved.)

    So, if I have a/ and I want to exclude a/b/c/*:

    mv a/b/c ../c
    mv a final_destination
    mkdir -p a/b
    mv ../c a/b/c
    

    Or something like that. Otherwise, you might be able to get find to help you.

    0 讨论(0)
  • 2020-12-23 11:47
    mv * exclude-dir
    

    was the perfect solution for me

    0 讨论(0)
  • 2020-12-23 11:53

    Lets's assume the dir structure is like,

    |parent
        |--child1
        |--child2
        |--grandChild1
        |--grandChild2
        |--grandChild3
        |--grandChild4
        |--grandChild5
        |--grandChild6
    

    And we need to move files so that it would appear like,

    |parent
        |--child1
        |   |--grandChild1
        |   |--grandChild2
        |   |--grandChild3
        |   |--grandChild4
        |   |--grandChild5
        |   |--grandChild6
        |--child2
    

    In this case, you need to exclude two directories child1 and child2, and move rest of the directories in to child1 directory.

    use,

    mv !(child1|child2) child1
    

    This will move all of rest of the directories into child1 directory.

    0 讨论(0)
  • 2020-12-23 11:54
    ls | grep -v exclude-dir | xargs -t -I '{}' mv {} exclude-dir
    
    0 讨论(0)
  • 2020-12-23 12:03

    This will move all files at or below the current directory not in the ./exclude/ directory to /wherever...

    find -E . -not -type d -and -not -regex '\./exclude/.*' -exec echo mv {} /wherever \;
    
    0 讨论(0)
  • 2020-12-23 12:12

    Since find does have an exclude option, use find + xargs + mv:

    find /source/directory -name ignore-directory-name -prune -print0 | xargs -0 mv --target-directory=/target/directory
    

    Note that this is almost copied from the find man page (I think using mv --target-directory is better than cpio).

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