2011-01-06 175 views

回答

52

讓我們假設目錄結構是這樣,

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

我們需要移動的文件,以便它看起來像,

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

在這種情況下,你需要exclud e兩個目錄child1child2,並將其餘目錄移至child1目錄。

使用,

mv !(child1|child2) child1 

這將所有目錄的其餘部分進入child1目錄。

1

這不正是你問什麼,但它可能做的工作:(本質上,將排除的文件夾出較大的樹被移動)

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 -p a/b 
mv ../c a/b/c 

或者類似的東西。否則,你可能會得到find來幫助你。

+0

如果你移動的文件在你的根目錄下?例如,一些系統只允許用戶訪問一個目錄(一些雲IDE是一個很好的例子)。也許有人想把所有東西都移到一個子目錄。這不起作用。 – Kallaste 2017-09-25 18:00:12

+0

正如我所說(六年前),答案並沒有描述確切要求什麼,而只是*可能*做這項工作。對於大多數情況下,它應該工作;當然,你的是一個例外。 – Thanatos 2017-09-25 23:00:08

+0

你說得對,它沒有。但我的意思並不是說這不是要求的,而是在某些情況下不起作用。我的例子只是一個假設來證明這一點。 – Kallaste 2017-09-26 04:27:42

3

由於發現確實有一個排除選項,請找+ xargs的+ MV:

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

注意,這幾乎是從查找手冊頁(我想用mv --target目錄複製優於的cpio)。

1

這不會在./exclude/目錄/何移動所有文件或當前目錄下...

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