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

David Liu picture David Liu · Jan 6, 2011 · Viewed 82.8k times · Source

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)?

Answer

Chathura Kulasinghe picture Chathura Kulasinghe · Aug 9, 2013

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.