2010-11-01 146 views
50

我想創建我們的多TB文件服務器結構的克隆。我知道cp -parents可以移動一個文件,它是父級結構,但有什麼方法可以完整地複製目錄結構嗎?將文件夾結構(無文件)從一個位置複製到另一個位置

我想複製到一個linux系統,我們的文件服務器上安裝了CIFS。

+0

的可能的複製[?Rsync的如何將其納入目錄,但沒有文件(https://stackoverflow.com/questions/3546001/rsync-how-to- include-directories-but-not-files) – 2017-06-01 20:14:56

回答

98

你可以這樣做:

find . -type d >dirs.txt 

創建目錄的列表,然後

xargs mkdir -p <dirs.txt 

上創建的目錄目的地。

+17

linux很棒 – user1167442 2014-03-18 00:03:19

+5

如果你的目錄名中有空格,這個解決方案將不起作用。 – Jealie 2015-03-11 20:35:46

+10

@Jealie將命令更改爲'find。 -type d -print0> dirs.txt'和'xargs -0 mkdir -p user1207177 2015-04-03 01:37:29

0

如果你可以從一臺Windows計算機訪問,您可以使用XCOPY與/ T和/ E只複製的文件夾結構(/ E包括空文件夾)

http://ss64.com/nt/xcopy.html

[編輯! ]

這一個使用rsync來重新創建目錄結構,但沒有文件。 http://psung.blogspot.com/2008/05/copying-directory-trees-with-rsync.html

實際上可能是更好的:)

+0

Linux,man。 Linux操作系統。不是窗口$。 – amphetamachine 2010-11-02 08:35:18

+1

不幸的是,我們的CIFS服務文件服務器沒有運行windows,所以沒有可以做win命令。 – r00fus 2010-11-08 23:04:29

+0

謝謝,rsync方法對我來說工作得很好。它也與目錄名稱中的空格兼容。 – Glutanimate 2012-12-07 00:40:41

50
cd /path/to/directories && 
find . -type d -exec mkdir -p -- /path/to/backup/{} \; 
+0

作品像魔術;) – Volvox 2014-02-11 10:58:52

+0

這是最終的建議! : – 2014-02-25 10:56:50

+2

找到我的最佳答案。否則,你可以嘗試從'rsync'解決方案從陳Levy答案在[這個SO問題](http://stackoverflow.com/questions/11946465/copy-a-directory-structure-with-file-names-without-content) – 2014-05-14 13:14:53

5

我不知道,如果你正在尋找一個在Linux上的解決方案。如果是這樣,你可以試試這個:

$ mkdir destdir 
$ cd sourcedir 
$ find . -type d | cpio -pdvm destdir 
+0

cpio似乎不適合我,至少使用您指定的參數。 – r00fus 2010-11-08 23:02:46

+0

@ r00fus - 請閱讀cpio手冊或參考http://www.gnu.org/software/cpio/ – zerodin 2010-11-08 23:10:08

1

替代target_dirsource_dir用適當的值:

cd target_dir && (cd source_dir; find . -type d ! -name .) | xargs -i mkdir -p "{}" 

測試在OSX + Ubuntu Linux系統。

1

以下解決方案運作良好,我在各種環境中:

sourceDir="some/directory" 
targetDir="any/other/directory" 

find "$sourceDir" -type d | sed -e "s?$sourceDir?$targetDir?" | xargs mkdir -p 
2

這工作:

find ./<SOURCE_DIR>/ -type d | sed 's/\.\/<SOURCE_DIR>//g' | xargs -I {} mkdir -p <DEST_DIR>"/{}" 

只需更換SOURCE_DIR和dest_dir將。

3

此副本的目錄和文件的屬性,而不是文件中的數據:

cp -R --attributes-only SOURCE DEST 

然後你就可以刪除的文件的屬性,如果你不感興趣:

find DEST -type f -exec rm {} \; 
+0

只是一個優秀的! – WebComer 2018-02-23 00:54:18

+0

會是非常優秀的,但您忘記提及保存所有權,時間戳和權限。所以它在Win7/cygwin中產生了一團糟 - NULL_SID用戶,權限順序錯誤,無法編輯權限等,無法訪問生成的文件結構。 – WebComer 2018-02-23 01:37:54

0

Python腳本來自Sergiy Kolodyazhnyy 發表於Copy only folders not files?

#!/usr/bin/env python 
import os,sys 
dirs=[ r for r,s,f in os.walk(".") if r != "."] 
for i in dirs: 
    os.makedirs(os.path.join(sys.argv[1],i)) 

或從外殼:

python -c 'import os,sys;dirs=[ r for r,s,f in os.walk(".") if r != "."];[os.makedirs(os.path.join(sys.argv[1],i)) for i in dirs]' ~/new_destination 

供參考:

9

下面是一個簡單的解決方案使用rsync:

rsync -av -f"+ */" -f"- *" "$source" "$target" 
  • 一行
  • 不帶空格的問題
  • 保存權限

I found this solution there

1

這解決了即使有空格的問題:

在原始/源代碼目錄:

find . -type d -exec echo "'{}'" \; > dirs2.txt 

然後重新創建它在新創建的目錄:

mkdir -p <../<SOURCEDIR>/dirs2.txt 
相關問題