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

LinuxMv

Linux Problem Overview


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

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

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

Solution 3 - Linux

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/

Solution 4 - Linux

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.

Solution 5 - Linux

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 \;

Solution 6 - Linux

#!/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'

Solution 7 - Linux

ls | grep -v exclude-dir | xargs -t -I '{}' mv {} exclude-dir

Solution 8 - Linux

rename your directory to make it hidden so the wildcard does not see it:

mv specific_dir .specific_dir 
mv * ../other_dir

Solution 9 - Linux

> Inspired by @user13747357 's answer.

First you can ls the file and filter them by:

ls | egrep -v '(dir_name|file_name.ext)'

Then you can run the following command to move the files except the specific ones:

mv $(ls | egrep -v '(dir_name|file_name.ext)') target_dir

* Note that I tested this inside a specific directory. Cross-directory operation should be more carefully executed :)

Solution 10 - Linux

mv * exclude-dir

was the perfect solution for me

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid LiuView Question on Stackoverflow
Solution 1 - LinuxChathura KulasingheView Answer on Stackoverflow
Solution 2 - LinuxSlartibartfastView Answer on Stackoverflow
Solution 3 - Linuxuser13747357View Answer on Stackoverflow
Solution 4 - LinuxThanatosView Answer on Stackoverflow
Solution 5 - LinuxsjrView Answer on Stackoverflow
Solution 6 - LinuxChris ReidView Answer on Stackoverflow
Solution 7 - LinuxxyzView Answer on Stackoverflow
Solution 8 - LinuxmarcView Answer on Stackoverflow
Solution 9 - Linuxkyon45View Answer on Stackoverflow
Solution 10 - LinuxFelix KörnerView Answer on Stackoverflow