如何使用“mv”命令来移动除特定目录下的文件?

时间:2022-06-02 19:52:33

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

我想知道-如何移动一个目录中的所有文件,除了特定目录中的那些文件(因为“mv”没有“——排除”选项)?

4 个解决方案

#1


55  

Lets's assume the dir structure is like,

假设dir结构是,

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

在这种情况下,您需要排除两个目录child1和child2,并将其余目录移到child1目录中。

use,

使用,

mv !(child1|child2) child1

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

这将把所有其他目录移动到child1目录中。

#2


3  

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

由于find确实有排除选项,使用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).

注意,这几乎是从find man页面复制的(我认为使用mv -target-directory比cpio要好)。

#3


1  

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/*:

所以,如果我有a/,我想排除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.

之类的。否则,你可能会找到帮助你的人。

#4


1  

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

#1


55  

Lets's assume the dir structure is like,

假设dir结构是,

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

在这种情况下,您需要排除两个目录child1和child2,并将其余目录移到child1目录中。

use,

使用,

mv !(child1|child2) child1

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

这将把所有其他目录移动到child1目录中。

#2


3  

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

由于find确实有排除选项,使用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).

注意,这几乎是从find man页面复制的(我认为使用mv -target-directory比cpio要好)。

#3


1  

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/*:

所以,如果我有a/,我想排除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.

之类的。否则,你可能会找到帮助你的人。

#4


1  

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