如何在 Linux 中符号链接文件?

发布于 2024-08-16 02:22:02 字数 1549 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

み零 2024-08-23 02:22:02

创建新的符号链接(如果符号链接已存在,则会失败):

ln -s /path/to/file /path/to/symlink

创建或更新符号链接:

ln -sf /path/to/file /path/to/symlink

To create a new symlink (will fail if symlink exists already):

ln -s /path/to/file /path/to/symlink

To create or update a symlink:

ln -sf /path/to/file /path/to/symlink
伴我老 2024-08-23 02:22:02
ln -s TARGET LINK_NAME

其中 -s 使其具有象征意义。

ln -s TARGET LINK_NAME

Where the -s makes it symbolic.

娇纵 2024-08-23 02:22:02
ln -s EXISTING_FILE_OR_DIRECTORY SYMLINK_NAME
ln -s EXISTING_FILE_OR_DIRECTORY SYMLINK_NAME
云归处 2024-08-23 02:22:02
ln -s target linkName

您可以在此处查看手册页:

http://linux.die.net/man/ 1/ln

ln -s target linkName

You can have a look at the man page here:

http://linux.die.net/man/1/ln

请恋爱 2024-08-23 02:22:02

(因为一张 ASCII 图片相当于一千个字符。)

箭头可能是一种有用的助记符,特别是因为它在 Emacs 的 dired 中几乎就是这样的。

以及大图,这样您就不会将其与 Windows 版本混淆

Linux:

ln -s target <- linkName

Windows:

mklink linkName -> target

您也可以将它们视为

ln -s "to-here" <- "from-here"
mklink "from-here" -> "to-here"

from-here 还不应该存在,它需要被创建,而to-here 应该已经存在 (IIRC)。

(我总是对各种命令和参数是否应该涉及预先存在的位置或要创建的位置感到困惑。)

编辑:对我来说,它仍然在慢慢地融入;我在笔记中写了另一种方法。

ln -s (target exists) (link is made)
mklink (link is made) (target exists)

(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:

ln -s target <- linkName

Windows:

mklink linkName -> target

You could also look at these as

ln -s "to-here" <- "from-here"
mklink "from-here" -> "to-here"

The from-here should not exist yet, it is to be created, while the to-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.

ln -s (target exists) (link is made)
mklink (link is made) (target exists)
生生不灭 2024-08-23 02:22:02
ln -s source_file target_file

http://unixhelp.ed.ac.uk/CGI/man-cgi?ln

ln -s source_file target_file

http://unixhelp.ed.ac.uk/CGI/man-cgi?ln

狼性发作 2024-08-23 02:22:02

对于最初的问题:


'ln -s '+basebuild+'/IpDome-kernel/kernel /home/build/sandbox/gen2/basebuild/IpDome-kernel/kernal'

这确实会创建一个 符号链接 (-s) 从文件/目录:

<basebuild>/IpDome-kernel/kernel

到您的新链接

/home/build/sandbox/gen2/basebuild/IpDome-kernel/kernal

这里有一些方法可以帮助您记住:

首先,有 ln 的手册页。您可以通过在谷歌中搜索“man ln”来访问此信息,或者只需打开终端窗口并输入 man ln 即可获得相同的信息。手册页明确指出:

<块引用>

ln [选项]... [-T] TARGET LINK_NAME(第一种形式)


如果您不适合每次都搜索或阅读手册页,也许您会更容易记住所有 nix命令的工作方式相同

cp /file/that/exists /location/for/new/file
mv /file/that/exists /location/its/moving/to
ln /file/that/exists /the/new/link

cp将当前存在的文件(第一个参数)复制到新文件(第二个参数)。
mv 移动当前存在的文件(第一个参数)到一个新地方(第二个参数)

同样 ln 链接当前存在的文件(第一个参数)到新链接(第二个参数)*


我想建议的最后一个选项是您可以创建自己的手册页,这些手册页易于阅读且易于(对您来说)查找/记住。只需制作一个简单的 shell 脚本即可为您提供所需的提示。例如

在您的 .bash_aliases 文件中,您可以放置​​类似以下内容:

commandsfx() {
    echo "Symlink:  ln -s /path/to/file /path/to/symlink"
    echo "Copy:     cp /file/to/copy /destination/to/send/copy"
}

alias 'cmds'=commandsfx

然后,当您需要它时,只需从命令行键入 cmds,您就会得到正确的信息语法以您可以快速阅读和理解的方式进行。您可以根据自己的需要将这些功能设置为高级,以获得所需的信息,这取决于您。您甚至可以使它们具有交互性,因此您只需按照提示进行操作...类似:

makesymlink() {
    echo "Symlink name:"
    read sym
    echo "File to link to:"
    read fil
    ln -s $fil $sym
}

alias 'symlink'=makesymlink

* - 显然它们都可以采用不同的参数并执行不同的操作,并且可以处理文件和目录...但是前提是一样的
♦ - 使用 bash shell 的示例

To the original question:


'ln -s '+basebuild+'/IpDome-kernel/kernel /home/build/sandbox/gen2/basebuild/IpDome-kernel/kernal'

This will indeed create a symbolic link (-s) from the file/directory:

<basebuild>/IpDome-kernel/kernel

to your new link

/home/build/sandbox/gen2/basebuild/IpDome-kernel/kernal

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 type man ln and you'll get the same information. The man page clearly states:

ln [OPTION]... [-T] TARGET LINK_NAME (1st form)


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 /file/that/exists /location/for/new/file
mv /file/that/exists /location/its/moving/to
ln /file/that/exists /the/new/link

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:

commandsfx() {
    echo "Symlink:  ln -s /path/to/file /path/to/symlink"
    echo "Copy:     cp /file/to/copy /destination/to/send/copy"
}

alias 'cmds'=commandsfx

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:

makesymlink() {
    echo "Symlink name:"
    read sym
    echo "File to link to:"
    read fil
    ln -s $fil $sym
}

alias 'symlink'=makesymlink

* - 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

静赏你的温柔 2024-08-23 02:22:02

在 Linux 上创建符号链接或软链接:

打开 Bash 提示符并键入下面提到的命令以创建指向文件的符号链接:

A) 转到要创建软链接的文件夹并按上述方式键入命令下面

$ ln -s (path-to-file) (symbolic-link-to-file)
$ ln -s /home/user/file new-file

B)转到新文件名路径并输入

$ ls -lrt(查看新文件是否链接到该文件)

示例:

user@user-DT:[~/Desktop/soft]# ln -s /home/user/Desktop/soft/File_B /home/user/Desktop/soft/File_C
user@user-DT:[~/Desktop/soft]# ls -lrt
total 0
-rw-rw-r-- 1 user user  0 Dec 27 16:51 File_B
-rw-rw-r-- 1 user user  0 Dec 27 16:51 File_A
lrwxrwxrwx 1 user user 31 Dec 27 16:53 File_C -> /home/user/Desktop/soft/File_B


Note: Where, File_C -> /home/user/Desktop/soft/File_B  Means, File_C is symbolically linked to File_B

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:

$ ln -s (path-to-file) (symbolic-link-to-file)
$ ln -s /home/user/file new-file

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:

user@user-DT:[~/Desktop/soft]# ln -s /home/user/Desktop/soft/File_B /home/user/Desktop/soft/File_C
user@user-DT:[~/Desktop/soft]# ls -lrt
total 0
-rw-rw-r-- 1 user user  0 Dec 27 16:51 File_B
-rw-rw-r-- 1 user user  0 Dec 27 16:51 File_A
lrwxrwxrwx 1 user user 31 Dec 27 16:53 File_C -> /home/user/Desktop/soft/File_B


Note: Where, File_C -> /home/user/Desktop/soft/File_B  Means, File_C is symbolically linked to File_B
情定在深秋 2024-08-23 02:22:02
ln [-Ffhinsv] source_file [target_file]

    link, ln -- make links

        -s    Create a symbolic link.

    A symbolic link contains the name of the file to which it is linked. 

    An ln command appeared in Version 1 AT&T UNIX.
ln [-Ffhinsv] source_file [target_file]

    link, ln -- make links

        -s    Create a symbolic link.

    A symbolic link contains the name of the file to which it is linked. 

    An ln command appeared in Version 1 AT&T UNIX.
与风相奔跑 2024-08-23 02:22:02

这是 Stack Overflow,所以我假设您需要代码:

以下所有代码都假设您想要创建一个名为 /tmp/link 的符号链接,该链接到 /tmp/realfile

警告:虽然此代码会检查错误,但它不会检查 /tmp/realfile 是否确实存在!这是因为死链接仍然有效,并且根据您的代码,您可能(很少)希望在真实文件之前创建链接。


Shell(bash、zsh...)

#!/bin/sh
ln -s /tmp/realfile /tmp/link

非常简单,就像您在命令行(即 shell)上执行的操作一样。所有错误处理均由 shell 解释器完成。此代码假设您在 /bin/sh 处有一个工作的 shell 解释器。

如果需要,您仍然可以使用 $? 变量实现自己的错误处理,如果链接成功创建,该变量只会设置为 0。

C 和 C++

#include <unistd.h>
#include <stdio.h>

int main () {
  if( symlink("/tmp/realfile", "/tmp/link") != 0 )
    perror("Can't create the symlink");
}

当可以创建链接时, symlink 仅返回 0。在其他情况下,我使用 perror 来详细说明问题。

Perl

#!/usr/bin/perl
if( symlink("/tmp/realfile", "/tmp/link") != 1) {
  print STDERR "Can't create the symlink: $!\n"
}

此代码假设您在 /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, ...)

#!/bin/sh
ln -s /tmp/realfile /tmp/link

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++

#include <unistd.h>
#include <stdio.h>

int main () {
  if( symlink("/tmp/realfile", "/tmp/link") != 0 )
    perror("Can't create the symlink");
}

symlink only returns 0 when the link can be created. In other cases I'm using perror to tell more about the problem.

Perl

#!/usr/bin/perl
if( symlink("/tmp/realfile", "/tmp/link") != 1) {
  print STDERR "Can't create the symlink: $!\n"
}

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.

何其悲哀 2024-08-23 02:22:02

ln -s sourcepath linkpathname

注意:

-s 生成符号链接而不是硬链接

ln -s sourcepath linkpathname

Note:

-s makes symbolic links instead of hard links

陌生 2024-08-23 02:22:02

如果您位于要创建符号链接的目录中,请忽略第二个路径。

cd myfolder
ln -s target

它将在 myfolder 内创建 target 的符号链接。

一般语法

ln -s TARGET LINK_NAME

If you are in the directory where you want to create symlink, then ignore second path.

cd myfolder
ln -s target

It will create symlink of target inside myfolder.

General syntax

ln -s TARGET LINK_NAME
梦魇绽荼蘼 2024-08-23 02:22:02

我想为已经提供的描述提供一个更简单的英语版本。

 ln -s  /path-text/of-symbolic-link  /path/to/file-to-hold-that-text

“ln”命令创建一个链接文件,“-s”指定链接类型为符号。符号链接文件的示例可以在 WINE 安装中找到(使用“ls -la”显示目录内容的一行):

 lrwxrwxrwx 1 me power 11 Jan  1 00:01 a: -> /mnt/floppy

标准文件信息内容位于左侧(尽管请注意第一个字符是“l”) ”代表“链接”);文件名是“a:”和“->”还表明该文件是一个链接。它主要告诉 WINE Windows“驱动器 A:”如何与 Linux 中的软盘驱动器关联。要实际创建一个与此类似的符号链接(在当前目录中,并且对 WINE 实际执行此操作更为复杂;使用“winecfg”实用程序):

 ln -s  /mnt/floppy  a:   //will not work if file a: already exists

I'd like to present a plainer-English version of the descriptions already presented.

 ln -s  /path-text/of-symbolic-link  /path/to/file-to-hold-that-text

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):

 lrwxrwxrwx 1 me power 11 Jan  1 00:01 a: -> /mnt/floppy

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):

 ln -s  /mnt/floppy  a:   //will not work if file a: already exists
一指流沙 2024-08-23 02:22:02

要创建符号链接/软链接,请使用:

ln -s {source-filename} {symbolic-filename}

例如:

ln -s file1 link1

To create a symbolic link /soft link, use:

ln -s {source-filename} {symbolic-filename}

e.g.:

ln -s file1 link1
偏闹i 2024-08-23 02:22:02

链接基本上有两种类型:

符号链接(软):链接到指示另一个文件的抽象位置的符号路径

硬链接:链接到物理数据的特定位置。

示例 1:

ln /root/file1 /root/file2

以上是硬链接的示例,您可以在其中获得物理数据的副本。

示例 2:

ln -s /path/to/file1.txt /path/to/file2.txt

上述命令将创建指向 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:

ln /root/file1 /root/file2

The above is an example of a hard link where you can have a copy of your physical data.

Example 2:

ln -s /path/to/file1.txt /path/to/file2.txt

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.

椵侞 2024-08-23 02:22:02

链接有两种类型:

符号链接:指的是指示另一个文件的抽象位置的符号路径

硬链接:指的是物理数据的具体位置。

在您的情况下符号链接:

ln -s source target

您可以参考 http://man7.org /linux/man-pages/man7/symlink.7.html

你可以创建太硬的链接

文件的硬链接与原始目录条目没有区别;对文件的任何更改实际上都独立于引用该文件的名称。硬链接通常不能引用目录,也不能跨越文件系统。

ln source link

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:

ln -s source target

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.

ln source link
微暖i 2024-08-23 02:22:02

我发现 man 信息中的术语“目标”和“目录”有点令人困惑。

目标是我们符号链接到的文件夹和实际符号链接的目录(不是您将符号链接到的目录),如果有人遇到同样的困惑,请不要感到孤独。

这是我对创建符号链接(在 Linux 中)的解释:

ln -s /FULL/PATH/FOLDER-OR-FILE-SYMLINKING-TO NAME-OF-YOUR-SYMLINK

您可以导航到要创建符号链接的文件夹并运行命令或指定符号链接的完整路径而不是您的符号链接的名称。

cd /FULL/PATH/TO/MY-SYMLINK-PARENT-FOLDER
ln -s /FULL/PATH/FOLDER-OR-FILE-SYMLINKING-TO NAME-OF-YOUR-SYMLINK

或者

ln -s /FULL/PATH/FOLDER-OR-FILE-SYMLINKING-TO /FULL/PATH/TO/MY-SYMLINK-PARENT-FOLDER/NAME-OF-YOUR-SYMLINK

我希望这对那些(仍然)有点困惑的人有所帮助。

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):

ln -s /FULL/PATH/FOLDER-OR-FILE-SYMLINKING-TO NAME-OF-YOUR-SYMLINK

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.

cd /FULL/PATH/TO/MY-SYMLINK-PARENT-FOLDER
ln -s /FULL/PATH/FOLDER-OR-FILE-SYMLINKING-TO NAME-OF-YOUR-SYMLINK

OR

ln -s /FULL/PATH/FOLDER-OR-FILE-SYMLINKING-TO /FULL/PATH/TO/MY-SYMLINK-PARENT-FOLDER/NAME-OF-YOUR-SYMLINK

I hope this helps to those (still) slighly confused.

蓝海似她心 2024-08-23 02:22:02

如何在 vagrant 中创建符号链接。
步骤:

  1. 在 vagrant 文件中创建一个同步文件夹。例如 config.vm.synced_folder "F:/Sunburst/source/sunburst/lms", "/source"
    F:/Sunburst/source/sunburst/lms :- 源代码所在,/source :- vagrant 内的目录路径
  2. Vagrant up 并输入 vagrant ssh 并转到源目录,例如 cd source
  3. 验证源代码文件夹结构在源目录。例如 /source/local
  4. 然后转到与浏览器关联的文件所在的来宾计算机目录。获得文件备份后。例如 sudo mv local local_bk
  5. 然后创建符号链接 例如 sudo ln -s /source/local local。本地平均链接名称(您要链接的访客计算机中的文件夹名称)
    如果您需要删除符号链接:- 输入 sudo rm local

How to create symlink in vagrant.
Steps:

  1. In vagrant file create a synced folder. e.g config.vm.synced_folder "F:/Sunburst/source/sunburst/lms", "/source"
    F:/Sunburst/source/sunburst/lms :- where the source code, /source :- directory path inside the vagrant
  2. Vagrant up and type vagrant ssh and go to source directory e.g cd source
  3. Verify your source code folder structure is available in the source directory. e.g /source/local
  4. Then go to the guest machine directory where the files which are associate with the browser. After get backup of the file. e.g sudo mv local local_bk
  5. Then create symlink e.g sudo ln -s /source/local local. local mean link-name (folder name in guest machine which you are going to link)
    if you need to remove the symlink :- Type sudo rm local
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文