Linux 将文件夹结构(不含文件)从一个位置复制到另一个位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4073969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy folder structure (without files) from one location to another
提问by r00fus
I want to create a clone of the structure of our multi-terabyte file server. I know that cp --parents can move a file and it's parent structure, but is there any way to copy the directory structure intact?
我想创建我们的多 TB 文件服务器结构的克隆。我知道 cp --parents 可以移动文件及其父结构,但是有没有办法完整地复制目录结构?
I want to copy to a linux system and our file server is CIFS mounted there.
我想复制到 linux 系统,我们的文件服务器安装在那里。
采纳答案by Greg Hewgill
You could do something like:
你可以这样做:
find . -type d > dirs.txt
to create the list of directories, then
创建目录列表,然后
xargs mkdir -p < dirs.txt
to create the directories on the destination.
在目标上创建目录。
回答by dotalchemy
If you can get access from a Windows machine, you can use xcopy with /T and /E to copy just the folder structure (the /E includes empty folders)
如果您可以从 Windows 计算机获得访问权限,则可以使用带有 /T 和 /E 的 xcopy 仅复制文件夹结构(/E 包括空文件夹)
[EDIT!]
[编辑!]
This one uses rsync to recreate the directory structure but without the files. http://psung.blogspot.com/2008/05/copying-directory-trees-with-rsync.html
这个使用 rsync 重新创建目录结构,但没有文件。 http://psung.blogspot.com/2008/05/copying-directory-trees-with-rsync.html
Might actually be better :)
实际上可能会更好:)
回答by amphetamachine
cd /path/to/directories &&
find . -type d -exec mkdir -p -- /path/to/backup/{} \;
回答by zerodin
I dunno if you are looking for a solution on Linux. If so, you can try this:
我不知道您是否正在寻找 Linux 上的解决方案。如果是这样,你可以试试这个:
$ mkdir destdir
$ cd sourcedir
$ find . -type d | cpio -pdvm destdir
回答by luissquall
Substitute target_dir
and source_dir
with the appropriate values:
用适当的值替换target_dir
和source_dir
:
cd target_dir && (cd source_dir; find . -type d ! -name .) | xargs -i mkdir -p "{}"
Tested on OSX+Ubuntu.
在 OSX+Ubuntu 上测试。
回答by yaccob
The following solution worked well for me in various environments:
以下解决方案在各种环境中对我都很有效:
sourceDir="some/directory"
targetDir="any/other/directory"
find "$sourceDir" -type d | sed -e "s?$sourceDir?$targetDir?" | xargs mkdir -p
回答by mijhael3000
This works:
这有效:
find ./<SOURCE_DIR>/ -type d | sed 's/\.\/<SOURCE_DIR>//g' | xargs -I {} mkdir -p <DEST_DIR>"/{}"
Just replace SOURCE_DIR and DEST_DIR.
只需替换 SOURCE_DIR 和 DEST_DIR。
回答by toliveira
This copy the directories and files attributes, but not the files data:
这会复制目录和文件属性,但不会复制文件数据:
cp -R --attributes-only SOURCE DEST
Then you can delete the files attributes if you are not interested in them:
然后,如果您对它们不感兴趣,可以删除文件属性:
find DEST -type f -exec rm {} \;
回答by Franck Dernoncourt
A python script from Sergiy Kolodyazhnyy posted on Copy only folders not files?:
来自 Sergiy Kolodyazhnyy 的 Python 脚本发布在仅复制文件夹而不是文件上?:
#!/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))
or from the shell:
或从外壳:
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
FYI:
供参考:
回答by Gildas
Here is a simple solution using rsync:
这是一个使用 rsync 的简单解决方案:
rsync -av -f"+ */" -f"- *" "$source" "$target"
- one line
- no problems with spaces
- preserve permissions
- 一条线
- 空格没有问题
- 保留权限