简体   繁体   English

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

[英]Copy folder structure (without files) from one location to another

I want to create a clone of the structure of our multi-terabyte file server.我想创建我们的多 TB 文件服务器结构的克隆。 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?我知道 cp --parents 可以移动文件及其父结构,但是有没有办法完整地复制目录结构?

I want to copy to a linux system and our file server is CIFS mounted there.我想复制到 linux 系统,我们的文件服务器安装在那里。

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.在目标上创建目录。

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

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 我在那里找到了这个解决方案

I dunno if you are looking for a solution on Linux.我不知道您是否正在寻找 Linux 上的解决方案。 If so, you can try this:如果是这样,你可以试试这个:

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

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

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。

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

This solves even the problem with whitespaces:这甚至解决了空格的问题:

In the original/source dir:在原始/源目录中:

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

then recreate it in the newly created dir:然后在新创建的目录中重新创建它:

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

Substitute target_dir and source_dir with the appropriate values:用适当的值替换target_dirsource_dir

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

Tested on OSX+Ubuntu.在 OSX+Ubuntu 上测试。

1 线解决方案:

find . -type d -exec mkdir -p /path/to/copy/directory/tree/{} \;

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.这个使用 rsync 重新创建目录结构,但没有文件。 http://psung.blogspot.com/2008/05/copying-directory-trees-with-rsync.html http://psung.blogspot.com/2008/05/copying-directory-trees-with-rsync.html

Might actually be better :)实际上可能会更好:)

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:仅供参考:

Another approach is use the tree which is pretty handy and navigating directory trees based on its strong options.另一种方法是使用非常方便的tree ,并根据其强大的选项导航目录树。 There are options for directory only, exclude empty directories, exclude names with pattern, include only names with pattern, etc. Check out man tree有仅目录选项,排除空目录,排除带有模式的名称,仅包含带有模式的名称等。查看man tree

Advantage : you can edit or review the list, or if you do a lot of scripting and create a batch of empty directories frequently优点:您可以编辑或查看列表,或者如果您经常编写大量脚本并创建一批空目录

Approach : create a list of directories using tree , use that list as an arguments input to mkdir方法:使用tree创建一个目录列表,将该列表用作mkdir的参数输入

tree -dfi --noreport > some_dir_file.txt

-dfi lists only directories, prints full path for each name, makes tree not print the indentation lines, -dfi仅列出目录,打印每个名称的完整路径,使树不打印缩进行,

--noreport Omits printing of the file and directory report at the end of the tree listing, just to make the output file not contain any fluff --noreport在树列表末尾打印文件和目录报告,只是为了使输出文件不包含任何绒毛

Then go to the destination where you want the empty directories and execute然后转到您想要空目录的目的地并执行

xargs mkdir < some_dir_file.txt
find source/ -type f  | rsync -a --exclude-from - source/ target/

仅复制具有相关权限和所有权的目录

简单的方法:

for i in `find . -type d`; do mkdir /home/exemplo/$i; done

Here is a solution in php that:这是php中的一个解决方案:

  • copies the directories (not recursively, only one level)复制目录(不是递归的,只有一层)
  • preserves permissions保留权限
  • unlike the rsync solution, is fast even with directories containing thousands of files as it does not even go into the folders与 rsync 解决方案不同,即使目录包含数千个文件,它也很快,因为它甚至不进入文件夹
  • has no problems with spaces空格没有问题
  • should be easy to read and adjust应该易于阅读和调整

Create a file like syncDirs.php with this content:使用以下内容创建一个类似syncDirs.php的文件:

<?php
foreach (new DirectoryIterator($argv[1]) as $f) {
    if($f->isDot() || !$f->isDir()) continue;
        mkdir($argv[2].'/'.$f->getFilename(), $f->getPerms());
        chown($argv[2].'/'.$f->getFilename(), $f->getOwner());
        chgrp($argv[2].'/'.$f->getFilename(), $f->getGroup());
}

Run it as user that has enough rights:以具有足够权限的用户身份运行它:

sudo php syncDirs.php /var/source /var/destination

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文件从一个位置复制到另一个位置,无需文件夹 - Copy files from one location to another without folder 一个用于将文件夹(不含文件)从一个位置复制到另一个位置的衬垫 - One liner to copy folders (without files) from one location to another 将文件(特定扩展名)从文件夹结构复制到另一个文件夹 - Copy files (of specific extension) from a folder structure to another folder 在linux中将文件列表从一个位置复制到另一个位置 - Copy list of files from one location to another in linux 在 Unix/Linux 中将特定文件从一个文件夹复制到另一个文件夹 - Copy specific files from one folder to another in Unix/Linux 复制文件以保留文件夹结构 - Copy files preserving folder structure 在Linux中将以“ a”,“ c”或“ e”开头的前N个文件从一个文件夹复制到另一个文件夹 - Copy first N files which start with 'a' 'c' or 'e' from one folder to another in Linux Linux 在 bash/python 脚本中将下一个“n”个文件从一个文件夹复制到另一个文件夹 - Linux copy next 'n' number of files from one folder to another in bash/python script 使用 python 和 mobaxterm 将 25 个随机文件从一个文件夹复制到另一个文件夹 - Use python with mobaxterm to copy 25 random files from one folder to another PHP将所有图像文件夹从一个文件夹复制到另一个文件夹 - PHP copy all images folder from one folder to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM