gpt4 book ai didi

linux - 如何使用查找创建子目录的副本

转载 作者:太空狗 更新时间:2023-10-29 11:12:36 25 4
gpt4 key购买 nike

我在 /home/ziga/Mounted/ 中有两个子目录。其中每一个都有多个非空子目录:

/home/ziga/Mounted/dir1/subdir1-1
/home/ziga/Mounted/dir1/subdir1-2
...

/home/ziga/Mounted/dir2/subdir2-1
/home/ziga/Mounted/dir2/subdir2-2
...

有什么方法可以使用 find 在其中创建 目录,比如 /home/ziga/CombinedMounts 具有完全相同的名称那些在 /home/ziga/Mounted/ 深处的两层?这是我正在寻找的最终结果:

/home/ziga/CombinedMounts/subdir1-1
/home/ziga/CombinedMounts/subdir1-2
/home/ziga/CombinedMounts/subdir2-1
/home/ziga/CombinedMounts/subdir2-2
...

我可以使用这个命令获取目录列表:

find /home/ziga/Mounted/ -maxdepth 2 -mindepth 2 -type d ! -name "lost+found" ! -name ".Trash*" -exec basename {} \;

但我终其一生都无法弄清楚如何在 mkdir -p 中使用它的输出,以便获得所需的子目录。

它们总是以某种方式结束于 /home/ziga/CombinedMounts/home/ziga/Mounted/...

我很迷茫,请帮忙。 :)

注意:部分目录名包含空格!

最佳答案

试试这个:

find /home/ziga/Mounted/ -maxdepth 2 -mindepth 2 \
-type d ! -name "lost+found" ! -name ".Trash*" \
-exec sh -c 'mkdir -p /home/ziga/CombinedMounts/"$(basename "$0")"' "{}" \;

我只会解释你在问题中没有提到的部分。

我们调用了 sh因为我们无法可靠地通过 {} mkdir -p "/path/$(basename "{}")" 等表达式的占位符.带有占位符的表达式看起来也不清楚。

-c选项设置参数值 0来自命令字符串后的操作数的值(请参阅 sh 的手册页):

-c Read commands from the command_string operand. Set the value of special parameter 0 (see Section 2.5.2, Special Parameters) from the value of the command_name operand and the positional parameters ($1, $2, and so on) in sequence from the remaining argument operands. No commands shall be read from the standard input.

关于linux - 如何使用查找创建子目录的副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40381574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com