符号链接,通常缩写为 symlink,是一种存储在您机器上的一个位置并指向同一台机器上的另一个位置的链接。您可以将其视为应用程序的快捷方式。即使实际的应用程序文件位于文件夹深处,您也可以简单地双击桌面上的应用程序快捷方式来启动应用程序。
符号链接是一种快捷方式,但它的工作方式(A symlink is a type of shortcut, but it works differently)与常规快捷方式不同。它不是一个快捷方式,而是它所指向的实际文件。您使用符号链接提供的任何应用程序都会将这些链接视为实际文件,而不是普通的快捷方式文件。
这些非常有用,因为您不必拘泥于特定文件夹即可使应用程序正常工作。您可以将数据存储在其他文件夹中,并且可以在原始文件夹中创建一个符号链接,指向您创建的新文件夹。您的系统和您的应用程序会认为您没有真正进行任何更改并且它们会正常工作,尽管情况并非如此。
使用终端创建符号链接(Creating a Symlink Using The
Terminal)
在Mac(Mac)上制作符号链接非常容易。内置的终端(Terminal)应用程序有一个命令,可让您在Mac上轻松创建任意数量的符号链接。
您只需要知道要创建符号链接的位置以及符号链接应指向的路径。获得此信息后,以下是在Terminal(Terminal)中创建符号链接的方法
。
在Mac(Mac)上使用您喜欢的方式启动终端(Terminal)应用程序。
在终端(Terminal)窗口中输入以下命令,然后按 Enter(Enter)。确保将目标(destination)替换为您希望链接指向的文件夹,并将位置(location)替换为您要保存链接的路径。
ln -s 目标位置(ln -s destination location)
要在桌面上创建指向 Documents 文件夹的符号链接,请使用以下命令:
ln -s /Users/Mahesh/Documents /Users/Mahesh/Desktop
将创建一个符号链接并将其保存在您的桌面上。双击它,它将在Finder中打开 Documents 文件夹(如果这是您在上面指定的) 。
如果要为其创建符号链接的目录的名称中有空格,请确保用双引号将路径名括起来以避免任何错误。
您现在可以在任何命令和应用程序中使用此符号链接,它将被视为您的文件夹或文件的实际版本(the actual version of your folder or file)。
使用应用程序创建符号链接(Use An App To Create a Symlink)
终端并不是在Mac(Mac)上创建符号链接的唯一方法。如果您碰巧不是终端用户(Terminal),您可以使用一个应用程序在您的机器上创建符号链接。
这个应用程序的作用是在您的上下文菜单中添加一个选项,因此您只需右键单击文件和文件夹即可创建符号链接。
前往 GitHub 上的SymbolicLinker 页面,在你的(SymbolicLinker page on GitHub)Mac上下载并打开包。
从包中复制SymbolicLinker.service.app文件,按住Option键,单击Finder 中的Go菜单,选择(Go)Library,打开Services文件夹,然后粘贴您复制的文件。
双击(Double-click)应用程序将其打开。它不会显示任何内容,但它已秘密地在您的上下文菜单中添加了一个选项。
找到要为其创建符号链接的文件或文件夹,右键单击它,然后选择Services ,然后选择Make Symbolic Link。
它将在与原始文件/文件夹相同的文件夹中创建符号链接。如果你愿意,你可以移动它。
(Create Symlinks Using)使用
Automator 服务(Automator Service)创建符号链接
Automator创建符号链接的方法与上述方法几乎相同。但是这个适合那些不信任互联网(Internet)上任何随机应用程序的人,你宁愿自己创建一些东西,这样你就知道它包含什么。
在 Mac 上启动Automator应用程序。
选择服务(Service),然后选择在您的(Choose)Mac上创建新的Automator服务。
将(Set)顶部的选项设置如下:
服务接收选定的 - 文件或文件夹
- 任何应用程序(Service receives selected – files or folders in – any application)
在操作列表中,搜索名为Run Shell Script的操作并将其拖到右侧面板上。
如下配置操作和命令:
Shell – /bin/bash Pass input – as arguments while [ $# -ne 0 ]; do ln -s “$1” “$1 symlink” shift done
通过单击顶部的文件菜单并选择(File)保存(Save)来保存服务。为服务输入一个有意义的名称,然后点击Save。
要使用新创建的Automator服务创建符号链接,请右键单击您的文件或文件夹,然后选择服务(Services),然后选择您的服务名称。
您还可以为该服务创建一个键盘快捷键(create a keyboard shortcut),以便在您的机器上更轻松地创建符号链接。
在 Mac 上删除符号链接(Deleting a Symlink On Mac)
符号链接不会占用太多内存空间,因为它们只是计算机上文件和文件夹的快捷方式。但是,如果您想从机器中删除其中的一个或几个,您有两种方法可以做到。
启动终端(Terminal)应用程序,输入以下命令,然后按 Enter(Enter)。确保将symlink替换为(symlink)Mac上符号链接的路径。
rm 符号链接(rm symlink)
另一种删除符号链接的方法是使用上下文菜单选项。右键单击您的符号链接并选择Move to Trash。它将从您的Mac中删除符号链接。
确保在删除符号链接后清空垃圾箱,以确保它从Mac中永久消失。
结论(Conclusion)
符号链接比常规别名更强大,因为它们在所有应用程序和命令中工作,就好像它们是真实文件一样。
How To Create Symlinks On Your Mac
A symbolic link, often shortened to symlink, is а type of link that is stored at one location on уour machine and points to another location on the ѕame machine. You can think оf it as a shortcut to an app. Even though the actual app file is located deep inside your folders, you can simplу double-click on the app shortcut on your desktop to launch the app.
A symlink is a type of shortcut, but it works differently than regular shortcuts. It’s less of a shortcut and more of the actual file it’s pointing to. Any app that you provide with your symlinks will think of these links as the actual files rather than normal shortcut files.
These are extremely useful as you don’t have
to be stuck to a particular folder for an app to work. You can have your data
stored in other folders and you can create a symlink in the original folder
pointing to the new folder you’ve created. Your system and your apps will think
you haven’t really made any changes and they’ll work normally, although things
are otherwise.
Creating a Symlink Using The
Terminal
Making a symlink on a Mac is extremely easy.
The built-in Terminal app has a command that lets you easily create as many
symlinks as you want on your Mac.
All that you need to know is the location
where you want to create the symlink and the path where the symlink should
point to. Once you have this information, here’s how you create a symlink in
Terminal.
Launch the Terminal app using your preferred way on your Mac.
Type in the following command into the Terminal window and hit Enter. Make sure to replace destination with the folder you want the link to point to and location with the path where you want to save the link.
ln -s destination location
To create a symlink on your desktop that points to your Documents folder, you’d use the following command:
ln -s /Users/Mahesh/Documents /Users/Mahesh/Desktop
A symlink will be created and saved on your desktop. Double-click on it and it’ll open the Documents folder (if that’s what you specified above) in the Finder.
If the directory you want to create a symlink for has spaces in its names, make sure to enclose the path names with double quotes to avoid any errors.
You can now use this symlink in any of your commands and apps and it’ll be considered as the actual version of your folder or file.
Use An App To Create a Symlink
The terminal isn’t the only way to create symlinks on your Mac. If you don’t happen to be a Terminal guy, you have an app available to let you create symlinks on your machine.
What this app does is it adds an option to
your context menu so you can create symlinks by just right-clicking on your
files and folders.
Head over to the SymbolicLinker page on GitHub and download and open the package on your Mac.
Copy the SymbolicLinker.service.app file from the package, hold down the Option key, click on the Go menu in the Finder, select Library, open the Services folder, and paste the file you copied.
Double-click on the app to open it. It won’t show anything but it has secretly added an option to your context menu.
Find the file or folder you want to create a symlink for, right-click on it, and select Services followed by Make Symbolic Link.
It’ll create the symlink in the same folder as
the original file/folder. You can move it around though if you want.
Create Symlinks Using An
Automator Service
The Automator method to create symlinks works
pretty much the same way as the above method. But this one will suit those of
you who don’t trust any random apps on the Internet, and you’d rather create
something by yourself so you know exactly what it contains.
Launch the Automator app on your Mac.
Select Service followed by Choose to create a new Automator service on your Mac.
Set the options at the top as the following:
Service receives selected – files or folders
in – any application
In the actions list, search for the action named Run Shell Script and drag it over to the right panel.
Configure the action and the commands as the following:
Shell – /bin/bash
Pass input – as arguments
while [ $# -ne 0 ]; do
ln -s “$1” “$1 symlink”
shift
done
Save the service by clicking on the File menu at the top and selecting Save. Enter a meaningful name for the service and hit Save.
To create a symlink with the newly created Automator service, right-click on your file or folder and select Services followed by your service name.
You can also create a keyboard shortcut for the service to make creating symlinks even easier on your machine.
Deleting a Symlink On Mac
Symlinks don’t occupy much memory space as
they’re just shortcuts to the files and folders on your machine. However, if
you want to remove one or a few of these from your machine, you have two ways
to do it.
Launch the Terminal app, type in the following command, and hit Enter. Make sure to replace symlink with the path of the symlink on your Mac.
rm symlink
Another way to delete a symlink is to use the context menu option. Right-click on your symlink and select Move to Trash. It’ll remove the symlink from your Mac.
Make sure to empty the Trash after you’re removed the symlink to ensure it’s gone for good from your Mac.
Conclusion
Symlinks are much more powerful than regular
aliases as these work in all the apps and commands as if these were the real
files.