命令行 上下箭头符号
Symbolic links allow you to links files and directories to other files and directories. They go by many names including symlinks, shell links, soft links, shortcuts, and aliases. At a glance, a symbolic link looks like just a file or directory, but when you interact with them, they will actually interact with the target at the other end. Think of them like worm holes for your file system.
使用符号链接可以将文件和目录链接到其他文件和目录。 它们有很多名称,包括符号链接,shell链接,软链接,快捷方式和别名。 乍一看,符号链接看起来就像一个文件或目录,但是当您与它们交互时,它们实际上将与另一端的目标交互。 将它们视为文件系统的蠕虫Kong。
入门 (Getting started)
The system call necessary to create symbolic links tends to be readily available on Unix-like and POSIX-compliant operating systems. The command we’ll be using to create the links is the ln
command.
创建符号链接所需的系统调用通常可以在类似Unix且兼容POSIX的操作系统上使用。 我们将用来创建链接的命令是ln
命令。
You’re welcome to use the files on your system to mess around with, but I thought it would be nice to provide a few lines of code to setup an environment for following along with this post:
欢迎您使用系统上的文件来解决问题,但是我认为最好提供几行代码来设置一个环境,以供后续工作:
$ mkdir -p /tmp/symlinks/{one,two}
$ cd /tmp/symlinks
$ echo "one" > ./one/one.txt
$ echo "two" > ./two/two.txt
If you were to run tree
, it should look like this:
如果要运行tree
,则应如下所示:
$ tree
.
├── one
│ └── one.txt
└── two
└── two.txt
2 directories, 2 files
Perfect, let’s get to linking!
完美,让我们开始链接!
硬连结 (Hard linking)
By default, the ln
command will make hard links instead of symbolic, or soft, links.
默认情况下, ln
命令将建立硬链接,而不是符号链接或软链接。
To ensure that we’re creating symbolic links, we’ll want to pass the -s
or --symbolic
argument to the ln
command.
为确保创建符号链接,我们将-s
或--symbolic
参数传递给ln
命令。
What’s a hard link anyway?
硬链接到底是什么?
Hard links link directly to the inode where the item resides on the disk, instead of acting as a pointer to the original file, the way symbolic links do. What you end up with is a copy of the original and any changes to the original will not be reflected.
硬链接直接链接到项目在磁盘上所在的索引节点,而不是像符号链接那样充当指向原始文件的指针。 您最终得到的是原件的副本,对原件的任何更改都不会反映出来。
Hard links serve their purpose in the world, but should be avoided when linking inside of of a git
repository as they cause a ton of confusion.
硬链接在世界上达到了他们的目的,但是在git
存储库内部进行链接时应避免使用硬链接,因为它们会引起很多混乱。
From my own experience, I’m always using symbolic links, but never hard links. As I write this, I’m wondering why I have yet to alias ln
to ln -s
to save a few keystrokes:
根据我自己的经验,我一直使用符号链接,但从未使用硬链接。 在撰写本文时,我想知道为什么我还没有将ln
别名为ln -s
来保存一些击键:
alias ls="ln -s"
符号链接 (Symbolic linking)
As mentioned, symbolic linking is effectively like creating a file that contains the target’s filename and path.
如前所述,符号链接实际上就像创建一个包含目标文件名和路径的文件。
Because it’s simply a reference to the original file, any changes that are made to the original will be immediately available via the symbolic link.
因为它只是对原始文件的引用,所以对原始文件所做的任何更改都可以通过符号链接立即获得。
Some of my favorite uses for symbolic links are to create local directories in my home directory that point to files being synchronized by Dropbox or to link current
to the latest build of a project in a directory that is dynamically named and includes a date and time.
我最喜欢的符号链接用法是在我的主目录中创建本地目录,该目录指向Dropbox正在同步的文件,或者将current
目录链接到动态命名的目录(包括日期和时间)中的项目的最新版本。
Given the files we created earlier in the getting started section, let’s go ahead and try linking the one
directory to the three
directory:
给定我们在入门一节中前面创建的文件,让我们继续尝试将one
目录链接到three
目录:
$ ln -s one three
Now we should have 3 directories, one of which is pointing back to another. To see things in more detail, we can use the ls
command:
现在我们应该有3个目录,其中一个指向另一个。 要更详细地了解事物,我们可以使用ls
命令:
$ ls
one/ three@ two/
Notice the @
symbol? That indicates that the file is a symbolic link!
注意@
符号吗? 这表明该文件是符号链接!
For even greater detail, we can pass in the -l
argument and see where the symbolic link is actually pointed:
有关更多详细信息,我们可以传入-l
参数,看看符号链接实际指向的位置:
$ ls -l
total 0
drwxr-xr-x 2 josh josh 60 Sep 13 17:18 one/
lrwxrwxrwx 1 josh josh 3 Sep 13 17:34 three -> one/
drwxr-xr-x 2 josh josh 60 Sep 13 17:18 two/
As expected, the three
link is pointed to the one
directory.
不出所料,这three
链接指向one
目录。
Symbolic links can also contain symbolic links, so we could go ahead and link the one.txt
file from three
to the two
directory:
符号链接也可以包含符号链接,因此我们可以将one.txt
文件从three
链接到two
目录:
$ ln -s three/one.txt two/one.txt
We should now have a file named one.txt
inside of the two
directory. Depending on your terminal configuration, this file very well may be blinking red.
现在,我们应该在two
目录中有一个名为one.txt
的文件。 根据您的终端配置,此文件可能会闪烁红色。
Even though the symbolic link was created, the way we specified the path was relative and thus, the link is broken because the two
directory doesn’t contain a three
directory with the one.txt
file in it.
即使创建了符号链接,我们指定路径的方式也是相对的,因此该链接已断开,因为two
目录中没有包含带有one.txt
文件的three
目录。
Fortunately, we can remedy this situation but telling ln
to create the symbolic link relative to the link location using the -r
or --relative
argument.
幸运的是,我们可以纠正这种情况,但可以告诉ln
使用-r
或--relative
参数创建相对于链接位置的符号链接。
Not so fast though, because the symbolic link already exists, we can’t overwrite it without passing in the -f
or --force
argument as well:
但是速度不是很快,因为符号链接已经存在,所以我们也不能不传递-f
或--force
参数就覆盖它:
$ ln -srf three/one.txt two/one.txt
Great! We now have two/one.txt
which was linked to three/one.txt
which is a link to one/one.txt
.
大! 现在,我们有two/one.txt
链接到three/one.txt
,后者是一个链接到one/one.txt
。
Phew, what a mouthful.
ew,真是满嘴。
Funny enough, your file system thinks it’s a mouthful as well. When we run tree
, the link target being shown is actually that of our original location and not to the link itself:
有趣的是,您的文件系统也认为这很麻烦。 当我们运行tree
,显示的链接目标实际上是我们原始位置的目标,而不是链接本身:
$ tree
.
├── one
│ └── one.txt
├── three -> one
└── two
├── one.txt -> ../one/one.txt
└── two.txt
3 directories, 3 files
Now that things are linked up nice, we can really see how symbolic links work by messing with the file contents.
现在事情已经很好地链接了,我们可以通过弄乱文件内容来真正了解符号链接的工作方式。
Here’s what all of our one.txt
files look like right now:
这是我们所有的one.txt
文件现在的样子:
$ cat {one,two,three}/one.txt
one
one
one
And if we were to update the contents of the original file and check again:
如果我们要更新原始文件的内容并再次检查:
$ echo "1. One" > one/one.txt
$ cat {one,two,three}/one.txt
1. One
1. One
1. One
Or even if we changed the contents via one of the symbolic links:
或者即使我们通过其中一个符号链接更改了内容:
$ echo "One and done" > three/one.txt
$ cat {one,two,three}/one.txt
One and done
One and done
One and done
Because symbolic links are simply pointers to files, any change we make, to the original file or the symbolic links will be immediately reflected in the original file or any symbolic links to the file.
因为符号链接只是指向文件的指针,所以我们对原始文件或符号链接所做的任何更改都将立即反映在原始文件或指向该文件的任何符号链接中。
结论 (Conclusion)
I hope you enjoyed this introduction to symbolic links. I do want to leave you with one word of caution though.
希望您喜欢符号链接的介绍。 不过,我确实想提醒您。
While linking is pretty great, and can make your life a ton easier, keep in mind that if you were to move or delete the original file or directory, all of your existing symbolic links pointed to it will become broken. There’s no automatic updating in that scenario.
虽然链接非常棒,并且可以使您的生活更轻松,但是请记住,如果要移动或删除原始文件或目录,则指向该文件或目录的所有现有符号链接都将断开。 在这种情况下,没有自动更新。
Hopefully this small short coming doesn’t scare you off from using this powerful command-line functionality!
希望这个短暂的机会不会吓到您使用此强大的命令行功能!
翻译自: https://www.digitalocean.com/community/tutorials/workflow-symbolic-links
命令行 上下箭头符号