bash 目标目录不存在时如何创建符号链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26379379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a symbolic link when target directory doesn't exist?
提问by dwkns
Is there a flag you can add to the ln
command to force the creation of the target directory structure (like mkdir -p
).
是否有一个标志可以添加到ln
命令中以强制创建目标目录结构(如mkdir -p
)。
Consider :
考虑 :
ln -s /Applications/'Sublime Text.app'/Contents/SharedSupport/bin/subl /usr/local/bin/
Which adds a symlink to the Sublime Text command line tool. But it fails if /usr/local/bin/ doesn't exist.
它为 Sublime Text 命令行工具添加了一个符号链接。但是如果 /usr/local/bin/ 不存在,它就会失败。
I've tried the -f
'force' flag but that doesn't help.
我试过-f
“强制”标志,但这没有帮助。
Do you need to test wether /usr/local/bin/ exists and create it if it doesn't before running the ln
command or is there a fancier way to do it?
您是否需要在运行ln
命令之前测试 /usr/local/bin/ 是否存在并创建它,如果它不存在,或者是否有更好的方法来做到这一点?
采纳答案by Adem ?zta?
Try like this,
试试这样,
mkdir -p /create_your_path/ && xargs ln -s /link_file_path/file /create_your_path/
回答by tripleee
Nope, there is no standard option to ln
to create a missing target directory. Use install
or mkdir -p
before ln
, perhaps in a helper script if you find you need this more than once.
不,没有ln
创建缺少的目标目录的标准选项。使用install
或mkdir -p
before ln
,如果您发现不止一次需要它,可以在帮助脚本中使用。
#!/bin/bash -e
mkdir -p "${!#}"
exec ln -s "$@"
This is obviously not very robust, but feel free to embellish it with more sanity checks if you think you really find it useful.
这显然不是很健壮,但如果你认为你真的觉得它有用,可以随意用更多的健全性检查来修饰它。