Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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/8/logging/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/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
Bash脚本自动创建指向树中子目录的符号链接_Bash_Whitespace_Symlink - Fatal编程技术网

Bash脚本自动创建指向树中子目录的符号链接

Bash脚本自动创建指向树中子目录的符号链接,bash,whitespace,symlink,Bash,Whitespace,Symlink,好的,这是我第三次尝试发布这个,也许我问错了问题 我已经好几年没有做任何shell编程了,所以我有点生疏了 我正在尝试创建一个简单的shell脚本,它可以在树中找到某个命名子目录下的所有子目录,并创建指向这些目录的符号链接(听起来比实际情况更令人困惑)。我正在Windows XP上使用cygwin 这个find/grep命令查找文件系统中的目录,就像我希望它: find -mindepth 3 -maxdepth 3 -type d | grep "New Parts" 现在是最难的部分。。。

好的,这是我第三次尝试发布这个,也许我问错了问题

我已经好几年没有做任何shell编程了,所以我有点生疏了

我正在尝试创建一个简单的shell脚本,它可以在树中找到某个命名子目录下的所有子目录,并创建指向这些目录的符号链接(听起来比实际情况更令人困惑)。我正在Windows XP上使用cygwin

这个find/grep命令查找文件系统中的目录,就像我希望它:

find -mindepth 3 -maxdepth 3 -type d | grep "New Parts"
现在是最难的部分。。。我只想把这个列表,导入ln并创建一些符号链接。目录列表中有一些空白,所以我尝试使用xargs来清理一些内容:

find -mindepth 3 -maxdepth 3 -type d | grep "New Parts" | xargs -0 ln -s -t /cygdrive/c/Views
不幸的是,ln会弹出一个很长的列表,列出所有连接在一起的目录(用\n分隔),并弹出一个“文件名太长”错误


想法???

我认为您可以在find命令内完成这一切。奥托姆:

find -mindepth 3 -maxdepth 3 -type d -name "*New Parts*" -exec ln -s -t /cygdrive/c/Views {} \;
希望我记住了正确的语法。

您的命令

find -mindepth 3 -maxdepth 3 -type d | grep "New Parts" | xargs -0 ln -s -t /cygdrive/c/Views
将参数“-0”设置为xargs,但您没有告诉find设置为“-print0”(如果您这样做,grep将无法在两者之间的管道中工作)。我想你想要的是:

find -mindepth 3 -maxdepth 3 -type d | grep "New Parts" | tr '\012' '\000' | xargs -0 ln -s -t /cygdrive/c/Views
tr
命令将换行符转换为ascii null。

使用for循环

for name in $(find $from_dir -mindepth 3 -maxdepth 3 -type d); do
  ln -s $name $to_dir
done
Xargs在管道输入位于命令末尾时出现问题。您需要的是多个命令,而不仅仅是一个命令


我在find命令中执行操作的经验有时会很慢,尽管它确实完成了任务。

如果找到的目录之一包含空格,则您的命令将失败。您必须添加引号,即。这是使用“xargs-0”的最大好处之一,可以避免所有此类问题。请使用
-name
而不是NWCoder的答案中的
grep
,并且可以使用
-print0
+1作为-exec标志。人们似乎忘记了这一点很多。您可能希望将{}括在引号中,这样它就可以处理带有空格的名称了。