Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

mv

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

like image 482
David Liu Avatar asked Jan 06 '11 05:01

David Liu


People also ask

Does mv move all files in directory?

The mv command is used to move files and directories from one place to another. We can also use it to rename files and directories. This will move all the files from /path/subfolder to /path/ except for hidden files and directories.

How do I select all files except one?

Click the first file or folder you want to select. Hold down the Shift key, select the last file or folder, and then let go of the Shift key. Hold down the Ctrl key and click any other file(s) or folder(s) you would like to add to those already selected.

How do I use mv command to move a file?

Use the mv command to move files and directories from one directory to another or to rename a file or directory. If you move a file or directory to a new directory without specifying a new name, it retains its original name. Attention: The mv command can overwrite many existing files unless you specify the -i flag.


1 Answers

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.

like image 168
Chathura Kulasinghe Avatar answered Sep 18 '22 16:09

Chathura Kulasinghe