如何在 Linux 中符号链接文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1951742/
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 can I symlink a file in Linux?
提问by chrissygormley
I want to make a symbolic link in Linux. I have written this Bash command where the first path is the folder I want link into and the second path is the compiled source.
我想在 Linux 中创建一个符号链接。我写了这个 Bash 命令,其中第一个路径是我想要链接到的文件夹,第二个路径是编译源。
ln -s '+basebuild+'/IpDome-kernel/kernel /home/build/sandbox/gen2/basebuild/IpDome-kernel/kernal
Is this correct?
这样对吗?
采纳答案by hsz
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
回答by rui
ln -s target linkName
You can have a look at the man page here:
您可以在此处查看手册页:
回答by cyborg
ln -s TARGET LINK_NAME
Where the -s
makes it symbolic.
凡-s
使得它的象征意义。
回答by miku
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.
回答by codaddict
ln -s EXISTING_FILE_OR_DIRECTORY SYMLINK_NAME
回答by Rob M
ln -s source_file target_file
回答by vernonner3voltazim
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):
“ln”命令创建一个链接文件,“-s”指定链接的类型将是符号的。在 WINE 安装中可以找到符号链接文件的示例(使用“ls -la”显示一行目录内容):
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):
标准文件信息位于左侧(但请注意,第一个字符是“链接”的“l”);文件名是“a:”,“->”也表示该文件是一个链接。它基本上告诉 WINE Windows“驱动器 A:”如何与 Linux 中的软盘驱动器相关联。要实际创建一个符号链接 SIMILAR(在当前目录中,实际为 WINE 执行此操作更复杂;使用“winecfg”实用程序):
ln -s /mnt/floppy a: //will not work if file a: already exists
回答by Fizer Khan
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
.
它将创建target
inside 的符号链接myfolder
。
General syntax
一般语法
ln -s TARGET LINK_NAME
回答by Brady Trainor
(Because an ASCII picture is worth a thousand characters.)
(因为一张 ASCII 图片值一千个字符。)
An arrow may be a helpful mnemonic, especially since that's almost exactly how it looks in Emacs' dired.
箭头可能是一个有用的助记符,尤其是因为它在 Emacs 的 dired 中几乎完全相同。
And big picture so you don't get it confused with the Windows' version
和大图,所以你不会把它与 Windows 版本混淆
Linux:
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).
本from-here
不应该存在,它是要创建,而to-here
应该已经存在(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)
回答by Mike
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:
这确实会从文件/目录创建一个符号链接( -s
):
<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
。您可以通过在 google 中搜索“man ln”来访问它,或者只需打开一个终端窗口并输入man ln
,您将获得相同的信息。手册页明确指出:
ln [OPTION]... [-T] TARGET LINK_NAME (1st form)
ln [OPTION]... [-T] TARGET LINK_NAME(第一种形式)
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:
如果每次都必须搜索或通读手册页不适合您,也许您会更容易记住所有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
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)*
cp
将当前存在的文件(第一个参数)复制到新文件(第二个参数)。mv
将当前存在的文件(第一个参数)移动到新位置(第二个参数)
同样ln
将当前存在的文件(第一个参数)链接到新链接(第二个参数)*
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?:
我想建议的最后一个选项是您可以创建自己的手册页,这些手册页易于阅读且易于(为您)查找/记住。只需制作一个简单的 shell 脚本,即可为您提供所需的提示。例如?:
In your .bash_aliases file you can place something like:
在您的 .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
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:
然后,当您需要它时,只需从命令行键入cmds
,您就会以一种可以快速阅读和理解的方式返回正确的语法。您可以根据需要将这些功能设置为高级,以获取您需要的信息,这取决于您。您甚至可以使它们具有交互性,因此您只需要按照提示进行操作即可……例如:
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
* - 很明显,它们都可以采用不同的参数并做不同的事情,并且可以处理文件和目录......但前提是相同的
?- 使用 bash shell 的示例