简体   繁体   English

ln -s和别名有什么区别?

[英]What's the difference between ln -s and alias?

I just found a workaround for a problem I was having with the subl command for Sublime Text 3 when the MacPorts version of python is installed. 我刚刚找到了一个解决方法, 解决了我在安装了MacPorts版本的python时使用Sublime Text 3的subl命令所遇到的问题 The instructions say to put a soft link, ln-s to the command line app in your /bin. 说明说要将软链接ln-s放到/ bin中的命令行应用程序中。 That didn't work, so I just opened my ~/.profile and added an alias: alias subl="/Applications/path/to/subl" . alias subl="/Applications/path/to/subl" ,所以我打开了我的〜/ .profile并添加了一个别名: alias subl="/Applications/path/to/subl" But that begs a new question for me. 但这对我来说是一个新问题。 What is the difference between these two: alias and soft links? 这两者有什么区别:别名和软链接?

They're entirely different things, though in this case they can be used for similar purposes. 它们是完全不同的东西,但在这种情况下它们可以用于类似的目的。

This: 这个:

alias subl="/Applications/path/to/subl"

creates an alias, so that typing subl as a shell command is equivalent to typing /Applications/path/to/subl . 创建别名,以便将subl键入shell命令等同于键入/Applications/path/to/subl

In bash, functions are generally preferred to aliases, because they're much more flexible and powerful. 在bash中,函数通常比别名更受欢迎,因为它们更灵活,更强大。

subl() { /Applications/path/to/subl ; }

Both these things are specific to the shell; 这两件事都是针对shell的; they cause the shell to expand sub1 to a specified command. 它们使shell将sub1扩展为指定的命令。

ln -s , on the other hand, creates a symbolic link in the file system. 另一方面, ln -s在文件系统中创建符号链接 A symbolic link is a reference to another file, and for most purposes it can be treated as if it were the file itself. 符号链接是对另一个文件的引用,对于大多数用途,它可以被视为文件本身。 It applies to anything that accesses it, not just to the shell, it's immediately visible to all processes running on the system, and it persists until it's removed. 它适用于访问它的任何东西,而不仅仅是shell,它对系统上运行的所有进程立即可见,并且一直持续到它被删除。 (A symbolic link is implemented as a small special file containing the name of the target file.) (符号链接实现为包含目标文件名称的小型特殊文件。)

An Alias is a Macintosh Finder concept . Alias是Macintosh Finder的概念 When you make an Alias in the Finder, the Finder tracks it. 在Finder中创建别名时,Finder会跟踪它。 When you move the original file or folder, the alias follows it. 移动原始文件或文件夹时,别名会跟随它。

A symbolic link is a Unix File System concept . 符号链接是Unix文件系统概念 When you make a symbolic link, it merely points to the original location. 当您创建符号链接时,它仅指向原始位置。 Move the original, and the symbolic link will point nowhere. 移动原始,符号链接将无处可寻。

When you use a Mac application, and use the Open/Save dialog box, it will handle aliases because it uses the Finder API, and the Finder handles alias tracking. 当您使用Mac应用程序并使用“打开/保存”对话框时,它将处理别名,因为它使用Finder API,Finder处理别名跟踪。

Unix tools don't integrate with the Finder API, so can't track aliases. Unix工具不与Finder API集成,因此无法跟踪别名。 However, they work with the underlying Unix API which handles symbolic links. 但是,它们使用处理符号链接的底层Unix API。 You can use ls on a symbolic link because it uses the Unix API. 您可以在符号链接上使用ls ,因为它使用Unix API。 Same with Python. 与Python相同。

Back in the System 7/8/9 days, the file system couldn't handle symbolic links much like the Windows API uses shortcuts and not symbolic links. 回到System 7/8/9天,文件系统无法处理符号链接,就像Windows API使用快捷方式而不是符号链接一样。 You needed aliases. 你需要别名。

However, Mac OS X is a Unix based OS, so understands the concept of symbolic links. 但是,Mac OS X是基于Unix的操作系统,因此理解符号链接的概念。 The Finder now treats symbolic links as it did aliases (except that symbolic links don't update when the original moves). Finder现在将符号链接视为别名(除了原始移动时符号链接不会更新)。 The only reason for aliases is to be compatible with the old Finder file system. 别名的唯一原因是与旧的Finder文件系统兼容。

It is really a super question 这真是一个超级问题

There are 3 levels of aliases in this debate 在这场辩论中有3个级别的别名

  1. File system: ln -s "target-file-or-directory" "alias" - this is visual for all programs using the file system (bash, Finder, applications) 文件系统: ln -s“target-file-or-directory”“alias” - 这对于使用文件系统的所有程序都是可视的(bash,Finder,应用程序)
  2. Shell alias: (bash/sh/zsh etc) - (part of question) - only used by shell command line Shell别名: (bash / sh / zsh等) - (部分问题) - 仅由shell命令行使用
  3. MacOS Finder: "make alias" - Known by Finder, and file dialogue box in most applications MacOS Finder: “make alias” - 由Finder知道,在大多数应用程序中都有文件对话框

Some different use cases: 一些不同的用例:

  • Want shell scripts (bash) to navigate your file system in a symbolic way - then use ln -s ... When you install java it will use this technique it self. 希望shell脚本(bash)以符号方式导航文件系统 - 然后使用ln -s ...当你安装java时 ,它将自己使用这种技术。 In example try to say which java and see where java is. 在示例中尝试说出哪个java并查看java所在的位置。 Then use ls -a /usr/bin/java to see where is really is. 然后使用ls -a / usr / bin / java来查看实际位置。
  • Want to do fast links in Finder so you can navigate to common things that happens to be located in different directories --> use Finder make alias 想在Finder中做快速链接,这样你就可以导航到恰好位于不同目录中的常见事物 - >使用Finder make alias
  • Want to start Sublime editor with a short cut from bash then use Shell alias . 想要使用bash的捷径启动Sublime编辑器,然后使用Shell别名 I have alias ll=ls -l - that lists a directory one item per line. 我有别名ll = ls -l - 每行列出一个项目。 I hardly cannot use bash without it :-) Note these substitutions only takes place on the command line substitution in bash and is therefore less useful in shell scripts. 没有它我几乎不能使用bash :-)注意这些替换只发生在bash中的命令行替换上,因此在shell脚本中不太有用。

Personally I use ln -s .. relative often. 我个人经常使用ln -s ..

I also use Finder make alias a lot. 我也使用Finder制作别名 It is easy and links follows the items as they more around. 它很容易,链接跟随项目,因为他们更多。 But it does not work from bash - therefore I sometimes changes these links to **ln -s ...* when I need to start scripting. 但它不能用bash工作 - 因此,当我需要开始编写脚本时,我有时会将这些链接更改为** ln -s ... *。

ln -s creates a symbolic link , which is almost a file on your filesystem ln -s创建一个符号链接 ,它几乎是文件系统上的一个文件

alias is a shell specific thing 别名是一个特定于shell的东西

So, basically, a symbolic link is a better solution because it is going to work for everything. 所以,基本上,符号链接是一个更好的解决方案,因为它将适用于所有事情。 Like, if you want to make your file manager open text files with that specific program, you can point it to your symbolic link and it is going to work. 就像,如果你想让你的文件管理器打开具有该特定程序的文本文件,你可以将它指向你的符号链接,它将会起作用。

Aliases only exists within the context of the shell (Bash, Sh, Zsh, etc.) but is not found in other applications whereas ln -s creates a virtual file (a link that is) to an existing real file that could present itself like a new command and should be cognizable by most applications that calls other binaries. 别名只存在于shell的上下文中(Bash,Sh,Zsh等),但在其他应用程序中找不到,而ln -s创建一个虚拟文件(一个链接)到现有的真实文件,它可以像一个新的命令,应该可以被大多数调用其他二进制文件的应用程序认知。 Aliases are similar to functions and variables only that they are more like command templates. 别名与函数和变量类似,只是它们更像命令模板。 Creating a function actually is more commendable. 创建一个函数实际上更值得称道。

I think you may be missing something in your alias command above -- it should have the form alias mumble="substitution" and will cause any command you type beginning with mumble to be replaced by substitution. 我认为您可能在上面的别名命令中遗漏了某些内容 - 它应该具有alias mumble="substitution"并且会导致您从mumble开始键入的任何命令被替换替换。 So if what you entered in your profile was alias subl="/Applications/path//to/subl" then whenever you type subl at the start of a command, it is replaced by the full path. 因此,如果您在配置文件中输入的是alias subl="/Applications/path//to/subl"那么无论何时在命令开头键入subl,它都会被完整路径替换。

ln works by creating a reference in the file system from one thing to another. ln通过创建文件系统从一件事到另一参考。

The link you provide above suggests that ln will not work with the version of Python provided in MacPorts. 您在上面提供的链接表明ln不适用于MacPorts中提供的Python版本。

EDIT: Another comment leads me to realize the alias I'm talking about is a mac-specific 'finder' alias, whereas the aliases in question here are bash 'shell' aliases. 编辑:另一个评论让我意识到我所说的别名是一个特定于mac的'finder'别名,而这里讨论的别名是bash'hell'别名。 My mistake. 我的错。

A symbolic, or soft, link points to a path: a location on the filesystem. 符号或软链接指向路径:文件系统上的位置。 If the file or folder located at the path is moved or renamed, the soft link will now point at nothing useful. 如果移动或重命名位于路径中的文件或文件夹,则软链接现在将无用。

An alias can contain a reference to a path, or to a file ID, or both, depending on the implementation. 别名可以包含对路径或文件ID或两者的引用,具体取决于实现。 On Mac OS X at least, the default is both, but the path is favoured over the file ID. 至少在Mac OS X上,默认值是两者,但路径优先于文件ID。 That is, as long as something exists at the path referenced by your alias, your alias will point to the path, just like a symbolic link does. 也就是说,只要别名引用的路径中存在某些内容,您的别名就会指向路径, 就像符号链接一样。 But, if nothing exists at the path referenced by your alias, it will instead point to the original file ID. 但是,如果别名引用的路径中不存在任何内容,则它将指向原始文件ID。

For example: 例如:

Suppose you create a file, and then create an alias for it, by specifying the file path. 假设您创建了一个文件,然后通过指定文件路径为其创建别名。 The alias now contains the file's file ID, as well as the file's path. 别名现在包含文件的文件ID以及文件的路径。 The alias will by default follow the file's path to take you to the file. 默认情况下,别名将按照文件的路径将您带到文件。

If you now move the file to a different location, the alias will follow it by referencing the file's file ID. 如果现在将文件移动到其他位置,则别名将通过引用文件的文件ID来跟随它。 But, if you assign a NEW file to the same file path as the old one, the alias will now point to the new file, since it favours path over file ID. 但是,如果将新文件分配给与旧文件相同的文件路径,则别名将指向新文件,因为它优先于文件ID的路径。

Reference: http://forums.macworld.com/index.php?/topic/142842-aliases-vs-symbolic-links/ 参考: http//forums.macworld.com/index.php?/topic / 142842-aliases-vs-symbolic-links/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM