将文件夹结构(无文件)从一个位置复制到另一个位置

时间:2022-03-14 06:25:35

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——父类可以移动文件和它的父类结构,但是有什么方法可以完整地复制目录结构吗?

I want to copy to a linux system and our file server is CIFS mounted there.

我想复制到linux系统,我们的文件服务器安装在那里。

11 个解决方案

#1


109  

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.

在目标上创建目录。

#2


56  

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

#3


13  

Here is a simple solution using rsync:

这里有一个使用rsync的简单解决方案:

rsync -av -f"+ */" -f"- *" "$source" "$target"
  • one line
  • 一行
  • no problems with spaces
  • 空间没有问题
  • preserve permissions
  • 保存权限

I found this solution there

我找到了这个解

#4


5  

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

#5


3  

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

#6


2  

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。

#7


1  

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

#8


1  

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。

#9


1  

This solves even the problem with whitespaces:

这甚至解决了白空间的问题:

In the original/source dir:

在最初的dir /来源:

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

then recreate it in the newly created dir:

然后在新创建的目录中重新创建它:

mkdir -p <../<SOURCEDIR>/dirs2.txt

#10


0  

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包含空文件夹)

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

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

[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 :)

可能会更好:)

#11


0  

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:

仅供参考:

#1


109  

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.

在目标上创建目录。

#2


56  

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

#3


13  

Here is a simple solution using rsync:

这里有一个使用rsync的简单解决方案:

rsync -av -f"+ */" -f"- *" "$source" "$target"
  • one line
  • 一行
  • no problems with spaces
  • 空间没有问题
  • preserve permissions
  • 保存权限

I found this solution there

我找到了这个解

#4


5  

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

#5


3  

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

#6


2  

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。

#7


1  

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

#8


1  

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。

#9


1  

This solves even the problem with whitespaces:

这甚至解决了白空间的问题:

In the original/source dir:

在最初的dir /来源:

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

then recreate it in the newly created dir:

然后在新创建的目录中重新创建它:

mkdir -p <../<SOURCEDIR>/dirs2.txt

#10


0  

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包含空文件夹)

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

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

[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 :)

可能会更好:)

#11


0  

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:

仅供参考: