如何使用'mv'命令来移动除特定目录以外的文件?

我想知道 – 如何移动目录中的所有文件,除了那些在特定目录中的文件(因为“MV”没有“–EXClude”选项)?

让我们假设目录结构就像,

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

我们需要移动文件,使其看起来像,

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

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

使用,

 mv !(child1|child2) child1 

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

由于find有一个排除选项,所以使用find + xargs + mv:

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

请注意,这几乎从查找手册页复制(我认为使用mv –target-directory比cpio好)。

这不是你所要求的,但它可能做到这一点:

 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 

(基本上,将排除的文件夹移出要移动的较大的树。)

所以,如果我有a/我想排除a/b/c/*

 mv a/b/c ../c mv a final_destination mkdir -pa/b mv ../ca/b/c 

或类似的东西。 否则,你可能会find帮助你。

这将把当前目录或下面的所有文件,而不是./exclude/目录中的所有文件移动到任何地方…

 find -E . -not -type d -and -not -regex '\./exclude/.*' -exec echo mv {} /wherever \;