This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(18)
创建新的符号链接(如果符号链接已存在,则会失败):
创建或更新符号链接:
To create a new symlink (will fail if symlink exists already):
To create or update a symlink:
其中
-s
使其具有象征意义。Where the
-s
makes it symbolic.您可以在此处查看手册页:
http://linux.die.net/man/ 1/ln
You can have a look at the man page here:
http://linux.die.net/man/1/ln
(因为一张 ASCII 图片相当于一千个字符。)
箭头可能是一种有用的助记符,特别是因为它在 Emacs 的 dired 中几乎就是这样的。
以及大图,这样您就不会将其与 Windows 版本混淆
Linux:
Windows:
您也可以将它们视为
from-here
还不应该存在,它需要被创建,而to-here
应该已经存在 (IIRC)。(我总是对各种命令和参数是否应该涉及预先存在的位置或要创建的位置感到困惑。)
编辑:对我来说,它仍然在慢慢地融入;我在笔记中写了另一种方法。
(Because an ASCII picture is worth a thousand characters.)
An arrow may be a helpful mnemonic, especially since that's almost exactly how it looks in Emacs' dired.
And big picture so you don't get it confused with the Windows' version
Linux:
Windows:
You could also look at these as
The
from-here
should not exist yet, it is to be created, while theto-here
should already exist (IIRC).(I always get mixed up on whether various commands and arguments should involve a pre-existing location, or one to be made.)
EDIT: It's still sinking in slowly for me; I have another way I've written in my notes.
http://unixhelp.ed.ac.uk/CGI/man-cgi?ln
http://unixhelp.ed.ac.uk/CGI/man-cgi?ln
对于最初的问题:
这确实会创建一个 符号链接 (
-s
) 从文件/目录:到您的新链接
这里有一些方法可以帮助您记住:
首先,有
ln
的手册页。您可以通过在谷歌中搜索“man ln”来访问此信息,或者只需打开终端窗口并输入man ln
即可获得相同的信息。手册页明确指出:如果您不适合每次都搜索或阅读手册页,也许您会更容易记住所有 nix命令的工作方式相同:
cp
将当前存在的文件(第一个参数)复制到新文件(第二个参数)。
mv
移动当前存在的文件(第一个参数)到一个新地方(第二个参数)同样
ln
链接当前存在的文件(第一个参数)到新链接(第二个参数)*我想建议的最后一个选项是您可以创建自己的手册页,这些手册页易于阅读且易于(对您来说)查找/记住。只需制作一个简单的 shell 脚本即可为您提供所需的提示。例如♦:
在您的 .bash_aliases 文件中,您可以放置类似以下内容:
然后,当您需要它时,只需从命令行键入
cmds
,您就会得到正确的信息语法以您可以快速阅读和理解的方式进行。您可以根据自己的需要将这些功能设置为高级,以获得所需的信息,这取决于您。您甚至可以使它们具有交互性,因此您只需按照提示进行操作...类似:* - 显然它们都可以采用不同的参数并执行不同的操作,并且可以处理文件和目录...但是前提是一样的
♦ - 使用 bash shell 的示例
To the original question:
This will indeed create a symbolic link (
-s
) from the file/directory:to your new link
Here's a few ways to help you remember:
First, there's the man page for
ln
. You can access this via searching "man ln" in google, or just open a terminal window and typeman ln
and you'll get the same information. The man page clearly states:If having to search or read through a man page every time isn't for you, maybe you'll have an easier time remembering that all nix commands work the same way:
cp
copies a file that currently exists (the first argument) to a new file (the second argument).mv
moves a file that currently exists (the first argument) to a new place (the second argument)Likewise
ln
links a file that currently exists (the first argument) to a new link (the second argument)*The final option I would like to suggest is you can create your own man pages that are easy to read and easy (for you) to find/remember. Just make a simple shell script that gives you the hint you need. For example♦:
In your .bash_aliases file you can place something like:
Then when you need it, from the command line just type
cmds
and you'll get back the proper syntax in a way you can quickly read and understand it. You can make these functions as advanced as you'd like to get what what information you need, it's up to you. You could even make them interactive so you just have to follow the prompts.. something like:* - well obviously they can all take different parameters and do different things and can work on files as well as directories... but the premise is the same
♦ - examples using the bash shell
在 Linux 上创建符号链接或软链接:
打开 Bash 提示符并键入下面提到的命令以创建指向文件的符号链接:
A) 转到要创建软链接的文件夹并按上述方式键入命令下面:
B)转到新文件名路径并输入:
$ ls -lrt(查看新文件是否链接到该文件)
示例:
Creating Symbolic links or Soft-links on Linux:
Open Bash prompt and type the below mentioned command to make a symbolic link to your file:
A) Goto the folder where you want to create a soft link and typeout the command as mentioned below:
B) Goto your new-file name path and type:
$ ls -lrt (To see if the new-file is linked to the file or not)
Example:
这是 Stack Overflow,所以我假设您需要代码:
以下所有代码都假设您想要创建一个名为
/tmp/link
的符号链接,该链接到/tmp/realfile
。警告:虽然此代码会检查错误,但它不会检查
/tmp/realfile
是否确实存在!这是因为死链接仍然有效,并且根据您的代码,您可能(很少)希望在真实文件之前创建链接。Shell(bash、zsh...)
非常简单,就像您在命令行(即 shell)上执行的操作一样。所有错误处理均由 shell 解释器完成。此代码假设您在
/bin/sh
处有一个工作的 shell 解释器。如果需要,您仍然可以使用
$?
变量实现自己的错误处理,如果链接成功创建,该变量只会设置为 0。C 和 C++
当可以创建链接时,
symlink
仅返回 0。在其他情况下,我使用perror
来详细说明问题。Perl
此代码假设您在
/usr/bin/perl
处有一个 perl 5 解释器。如果可以创建链接,symlink
仅返回 1。在其他情况下,我将失败原因打印到标准错误输出。This is Stack Overflow so I assume you want code:
All following code assumes that you want to create a symbolic link named
/tmp/link
that links to/tmp/realfile
.CAUTION: Although this code checks for errors, it does NOT check if
/tmp/realfile
actually exists ! This is because a dead link is still valid and depending on your code you might (rarely) want to create the link before the real file.Shell (bash, zsh, ...)
Real simple, just like you would do it on the command line (which is the shell). All error handling is done by the shell interpreter. This code assumes that you have a working shell interpreter at
/bin/sh
.If needed you could still implement your own error handling by using the
$?
variable which will only be set to 0 if the link was successfully created.C and C++
symlink
only returns 0 when the link can be created. In other cases I'm usingperror
to tell more about the problem.Perl
This code assumes you have a perl 5 interpreter at
/usr/bin/perl
.symlink
only returns 1 if the link can be created. In other cases I'm printing the failure reason to the standard error output.ln -s sourcepath linkpathname
注意:
-s 生成符号链接而不是硬链接
ln -s sourcepath linkpathname
Note:
-s makes symbolic links instead of hard links
如果您位于要创建符号链接的目录中,请忽略第二个路径。
它将在
myfolder
内创建target
的符号链接。一般语法
If you are in the directory where you want to create symlink, then ignore second path.
It will create symlink of
target
insidemyfolder
.General syntax
我想为已经提供的描述提供一个更简单的英语版本。
“ln”命令创建一个链接文件,“-s”指定链接类型为符号。符号链接文件的示例可以在 WINE 安装中找到(使用“ls -la”显示目录内容的一行):
标准文件信息内容位于左侧(尽管请注意第一个字符是“l”) ”代表“链接”);文件名是“a:”和“->”还表明该文件是一个链接。它主要告诉 WINE Windows“驱动器 A:”如何与 Linux 中的软盘驱动器关联。要实际创建一个与此类似的符号链接(在当前目录中,并且对 WINE 实际执行此操作更为复杂;使用“winecfg”实用程序):
I'd like to present a plainer-English version of the descriptions already presented.
The "ln" command creates a link-FILE, and the "-s" specifies that the type of link will be symbolic. An example of a symbolic-link file can be found in a WINE installation (using "ls -la" to show one line of the directory contents):
Standard file-info stuff is at left (although note the first character is an "l" for "link"); the file-name is "a:" and the "->" also indicates the file is a link. It basically tells WINE how Windows "Drive A:" is to be associated with a floppy drive in Linux. To actually create a symbolic link SIMILAR to that (in current directory, and to actually do this for WINE is more complicated; use the "winecfg" utility):
要创建符号链接/软链接,请使用:
例如:
To create a symbolic link /soft link, use:
e.g.:
链接基本上有两种类型:
符号链接(软):链接到指示另一个文件的抽象位置的符号路径
硬链接:链接到物理数据的特定位置。
示例 1:
以上是硬链接的示例,您可以在其中获得物理数据的副本。
示例 2:
上述命令将创建指向 file1.txt 的符号链接。
如果您删除源文件,那么您将不会有任何内容到达软目的地。
当您执行以下操作时:
ls -lai
您将看到符号链接有不同的 inode 编号。
有关更多详细信息,您可以阅读 Linux 操作系统上 ln 的手册页。
Links are basically of two types:
Symbolic links (soft): link to a symbolic path indicating the abstract location of another file
Hard links: link to the specific location of physical data.
Example 1:
The above is an example of a hard link where you can have a copy of your physical data.
Example 2:
The above command will create a symbolic link to file1.txt.
If you delete a source file then you won't have anything to the destination in soft.
When you do:
ls -lai
You'll see that there is a different inode number for the symlinks.
For more details, you can read the man page of ln on your Linux OS.
链接有两种类型:
符号链接:指的是指示另一个文件的抽象位置的符号路径
硬链接:指的是物理数据的具体位置。
在您的情况下符号链接:
您可以参考 http://man7.org /linux/man-pages/man7/symlink.7.html
你可以创建太硬的链接
文件的硬链接与原始目录条目没有区别;对文件的任何更改实际上都独立于引用该文件的名称。硬链接通常不能引用目录,也不能跨越文件系统。
There are two types of links:
symbolic links: Refer to a symbolic path indicating the abstract location of another file
hard links: Refer to the specific location of physical data.
In your case symlinks:
you can refer to http://man7.org/linux/man-pages/man7/symlink.7.html
you can create too hard links
A hard link to a file is indistinguishable from the original directory entry; any changes to a file are effectively independent of the name used to reference the file. Hard links may not normally refer to directories and may not span file systems.
我发现 man 信息中的术语“目标”和“目录”有点令人困惑。
目标是我们符号链接到的文件夹和实际符号链接的目录(不是您将符号链接到的目录),如果有人遇到同样的困惑,请不要感到孤独。
这是我对创建符号链接(在 Linux 中)的解释:
您可以导航到要创建符号链接的文件夹并运行命令或指定符号链接的完整路径而不是您的符号链接的名称。
或者
我希望这对那些(仍然)有点困惑的人有所帮助。
I find a bit confusing the terminologies "target" and "directory" in the man information.
The target is the folder that we are symlinking to and the directory the actual symlink (not the directory that you will be symlinking to), if anyone is experiencing the same confusion, don't feel alone.
This is my interpretation of creating a Symlink (in linux):
You can navigate to the folder where you want to create the symlink and run the command or specify the FULL PATH for your symlink instead of NAME-OF-YOUR-SYMLINK.
OR
I hope this helps to those (still) slighly confused.
如何在 vagrant 中创建符号链接。
步骤:
F:/Sunburst/source/sunburst/lms :- 源代码所在,/source :- vagrant 内的目录路径
如果您需要删除符号链接:- 输入 sudo rm local
How to create symlink in vagrant.
Steps:
F:/Sunburst/source/sunburst/lms :- where the source code, /source :- directory path inside the vagrant
if you need to remove the symlink :- Type sudo rm local