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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:34:17  来源:igfitidea点击:

How to create a symbolic link when target directory doesn't exist?

bashunixterminal

提问by dwkns

Is there a flag you can add to the lncommand 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 lncommand 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 lnto create a missing target directory. Use installor mkdir -pbefore ln, perhaps in a helper script if you find you need this more than once.

不,没有ln创建缺少的目标目录的标准选项。使用installmkdir -pbefore 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.

这显然不是很健壮,但如果你认为你真的觉得它有用,可以随意用更多的健全性检查来修饰它。