bash ln -s 和别名有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18452596/
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
What's the difference between ln -s and alias?
提问by charltoons
I just found a workaround for a problemI was having with the subl
command for Sublime Text 3 when the MacPorts version of python is installed. The instructions say to put a soft link, ln-s
to the command line app in your /bin. That didn't work, so I just opened my ~/.profile and added an alias:
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?
我只是找到了一个解决方法的问题,我与有subl
Python安装的版本MacPorts的时崇高的文本3命令。说明说要ln-s
在 /bin 中放置一个指向命令行应用程序的软链接。那没有用,所以我只是打开了我的 ~/.profile 并添加了一个别名:
alias subl="/Applications/path/to/subl"
. 但这对我来说提出了一个新问题。这两者有什么区别:别名和软链接?
采纳答案by Keith Thompson
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; they cause the shell to expand sub1
to a specified command.
这两个东西都是特定于 shell 的;它们会导致 shell 扩展sub1
为指定的命令。
ln -s
, on the other hand, creates a symbolic linkin the file system. 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. (A symbolic link is implemented as a small special file containing the name of the target file.)
ln -s
,另一方面,在文件系统中创建一个符号链接。符号链接是对另一个文件的引用,在大多数情况下,它可以被视为文件本身。它适用于访问它的任何东西,而不仅仅是外壳,它对系统上运行的所有进程立即可见,并且一直存在,直到它被删除。(符号链接被实现为一个包含目标文件名称的小型特殊文件。)
回答by David W.
An Alias is a Macintosh Finder concept. When you make an Alias in the Finder, the Finder tracks it. When you move the original file or folder, the alias follows it.
别名是Macintosh Finder 的概念。当您在 Finder 中创建别名时,Finder 会跟踪它。当您移动原始文件或文件夹时,别名跟随它。
A symbolic link is a Unix File System concept. When you make a symbolic link, it merely points to the original location. Move the original, and the symbolic link will point nowhere.
符号链接是Unix 文件系统的概念。当您创建符号链接时,它仅指向原始位置。移动原件,符号链接将无处可去。
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. However, they work with the underlying Unix API which handles symbolic links. You can use ls
on a symbolic link because it uses the Unix API. Same with Python.
Unix 工具不与 Finder API 集成,因此无法跟踪别名。但是,它们与处理符号链接的底层 Unix API 一起使用。您可以ls
在符号链接上使用,因为它使用 Unix API。与 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. You needed aliases.
回到系统 7/8/9 天,文件系统无法处理符号链接,就像 Windows API 使用快捷方式而不是符号链接一样。你需要别名。
However, Mac OS X is a Unix based OS, so understands the concept of symbolic links. The Finder now treats symbolic links as it did aliases (except that symbolic links don't update when the original moves). The only reason for aliases is to be compatible with the old Finder file system.
但是,Mac OS X 是基于 Unix 的操作系统,因此了解符号链接的概念。Finder 现在像对待别名一样对待符号链接(除了符号链接在原始移动时不会更新)。别名的唯一原因是与旧的 Finder 文件系统兼容。
回答by Anders K.
It is really a super question
这真是一个超级问题
There are 3 levels of aliases in this debate
这场辩论有 3 个级别的别名
- File system:ln -s "target-file-or-directory" "alias" - this is visual for all programs using the file system (bash, Finder, applications)
- Shell alias:(bash/sh/zsh etc) - (part of question) - only used by shell command line
- MacOS Finder:"make alias" - Known by Finder, and file dialogue box in most applications
- 文件系统:ln -s "target-file-or-directory" "alias" - 这对于使用文件系统的所有程序(bash、Finder、应用程序)都是可视的
- Shell 别名:(bash/sh/zsh 等)-(问题的一部分)- 仅由 shell 命令行使用
- MacOS Finder:“制作别名” - 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 javait will use this technique it self. In example try to say which javaand see where java is. Then use ls -a /usr/bin/javato see where is really is.
- 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
- Want to start Sublime editor with a short cut from bash then use Shell alias. I have alias ll=ls -l- that lists a directory one item per line. I hardly cannot use bashwithout it :-) Note these substitutions only takes place on the command line substitution in bashand is therefore less useful in shell scripts.
- 希望 shell 脚本 (bash) 以符号方式导航您的文件系统 - 然后使用ln -s ...当您安装java 时,它将自行使用此技术。在示例中尝试说出哪个 java并查看 java 在哪里。然后使用ls -a /usr/bin/java看看它到底在哪里。
- 想要在 Finder 中进行快速链接,以便您可以导航到恰好位于不同目录中的常见内容 --> 使用Finder make alias
- 想要通过 bash 的快捷方式启动 Sublime 编辑器,然后使用Shell alias。我有别名ll=ls -l- 每行列出一个目录。没有它我几乎不能使用bash:-) 注意这些替换只发生在bash的命令行替换中,因此在 shell 脚本中不太有用。
Personally I use ln -s ..relative often.
我个人经常使用ln -s ..relative 。
I also use Finder make aliasa lot. 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.
我也经常使用Finder 制作别名。这很容易,链接会随着项目的增加而跟随。但是它在bash 中不起作用- 因此当我需要开始编写脚本时,我有时会将这些链接更改为 **ln -s ...*。
回答by Aleks-Daniel Jakimenko-A.
ln -s
creates a symbolic link, which is almost a file on your filesystem
ln -s
创建一个符号链接,它几乎是您文件系统上的一个文件
aliasis 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.
因此,基本上,符号链接是更好的解决方案,因为它适用于所有情况。就像,如果你想让你的文件管理器用那个特定的程序打开文本文件,你可以将它指向你的符号链接,它就会起作用。
回答by konsolebox
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. Aliases are similar to functions and variables only that they are more like command templates. Creating a function actually is more commendable.
别名仅存在于 shell(Bash、Sh、Zsh 等)的上下文中,但在其他应用程序中找不到,而ln -s
创建一个虚拟文件(即链接)到现有的真实文件,该文件可以像新命令一样呈现自己并且应该可以被大多数调用其他二进制文件的应用程序识别。别名与函数和变量相似,只是它们更像命令模板。创建一个函数其实更值得称道。
回答by Beel
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. 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 mumble="substitution"
并且会导致你键入的任何以 mumble 开头的命令被替换替换。因此,如果您在配置文件中输入的内容是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 版本。
回答by Masterfool
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. My mistake.
编辑:另一条评论让我意识到我正在谈论的别名是特定于 mac 的“finder”别名,而此处所讨论的别名是 bash“shell”别名。我的错。
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. On Mac OS X at least, the default is both, but the path is favoured over the file 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 的引用,或两者都包含,具体取决于实现。至少在 Mac OS X 上,默认值是两者,但路径优先于文件 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. The alias will by default follow the file's path to take you to the file.
假设您创建了一个文件,然后通过指定文件路径为其创建别名。别名现在包含文件的文件 ID 以及文件的路径。默认情况下,别名将跟随文件的路径将您带到该文件。
If you now move the file to a different location, the alias will follow it by referencing the file's file 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 来跟随它。但是,如果您将新文件分配给与旧文件相同的文件路径,则别名现在将指向新文件,因为它更喜欢路径而不是文件 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/