简体   繁体   English

复制没有文件的目录结构

[英]copy directory structure without files

Hello i am trying to make a script which takes two arguments (names of directories) and copies the structure of the first into the other.您好,我正在尝试制作一个脚本,该脚本需要两个 arguments(目录名称)并将第一个的结构复制到另一个中。

This is my code:这是我的代码:

cd $1 && find . -type d -exec mkdir -p /$2/{} \;

when i run the script i dont get any errors but nothing happens.当我运行脚本时,我没有收到任何错误,但没有任何反应。 What am i doing wrong please help thank you.我在做什么错请帮助谢谢。

edit: the script is saved in home and both directories are also in home (~).编辑:脚本保存在主目录中,两个目录也在主目录(~)中。 i run the script in terminal:我在终端中运行脚本:

sudo bash DN1c.sh dir1 dir2

first directory has multiple subdirectories and the second directory is empty第一个目录有多个子目录,第二个目录为空

If the target is a relative path your command will get into an infinite loop.如果目标是相对路径,您的命令将进入无限循环。 Below one doesn't have that problem.下面的没有这个问题。

find "$1/." -type d -exec bash -c '
mkdir -p "${@/*\/.\//"$0"/}"' "$2" {} +
  • Uses $1/.使用$1/. as the starting point so that /./ marks where source path ends in each path.作为起点,以便/./标记源路径在每个路径中的结束位置。
  • Relies on bash's pattern substitution PE;依赖 bash 的模式替换 PE; "${@/*\/.\//"$0"/}" replaces the longest match of */./ with the target path in each member of $@ (ie paths selected) and expands to the resultant list. "${@/*\/.\//"$0"/}"*/./的最长匹配替换为$@每个成员中的目标路径(即选择的路径),并扩展为结果列表。
  • Abuses $0 a bit but it won't cause any harm.稍微滥用$0 ,但不会造成任何伤害。

Handling of positional parameters is left to OP.位置参数的处理留给 OP。

You could use rsync to copy files and/or directories.您可以使用rsync复制文件和/或目录。

To copy directories only, set --max-size=0 and no files will be copied.要仅复制目录,请设置--max-size=0并且不会复制任何文件。

Example:例子:

rsync -r -n -v --max-size=0 src_path/ dest_path

       ^                                        recursive
          ^                                     dry run - nothing copied
             ^                                  verbose
                     ^                          no files
                              ^                 src path
                                    ^           use a trailing / if you don't 
                                                want the src_path created
                                                at dest
                                           ^    dest path 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM