Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何使用find创建子目录的副本_Linux_Bash_Shell_Sh - Fatal编程技术网

Linux 如何使用find创建子目录的副本

Linux 如何使用find创建子目录的副本,linux,bash,shell,sh,Linux,Bash,Shell,Sh,我在/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中创建空的目录,这些目录的名称与/ho

我在
/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
从命令字符串操作数读取命令。根据
命令_name
操作数的值和位置参数(
$1
$2
,等等)按顺序设置特殊参数0的值(参见第2.5.2节,特殊参数)。不得从标准输入读取任何命令


能否将结果设置为数组的元素?比如
DIRS=(``)
。然后在$DIRS中为dir使用
来生成每个目录。如果目录名中有空格,这会起作用吗?哦,可能不会。我不是bash高手,只是在这里吐球。太棒了!就是这样!:)感谢您的详细回答,特别是各种引号和括号的正确位置;)那些总是让我生气。在撞了我几个小时后,你在几分钟内给了我解决方案。再次感谢!