What's the difference between ln -s and alias?

后端 未结 7 1989
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-23 09:42

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. The instructions

相关标签:
7条回答
  • 2020-12-23 10:08

    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.

    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.

    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.

    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.

    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.

    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.

    0 讨论(0)
  • 2020-12-23 10:09

    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.

    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.

    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.

    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.

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

    0 讨论(0)
  • 2020-12-23 10:12

    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.

    In bash, functions are generally preferred to aliases, because they're much more flexible and powerful.

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

    Both these things are specific to the shell; they cause the shell to expand sub1 to a specified command.

    ln -s, on the other hand, creates a symbolic link in 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.)

    0 讨论(0)
  • 2020-12-23 10:16

    It is really a super question

    There are 3 levels of aliases in this debate

    1. File system: ln -s "target-file-or-directory" "alias" - this is visual for all programs using the file system (bash, Finder, applications)
    2. Shell alias: (bash/sh/zsh etc) - (part of question) - only used by shell command line
    3. MacOS Finder: "make alias" - Known by Finder, and file dialogue box in most applications

    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. In example try to say which java and see where java is. Then use ls -a /usr/bin/java to 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 bash without it :-) Note these substitutions only takes place on the command line substitution in bash and is therefore less useful in shell scripts.

    Personally I use ln -s .. relative often.

    I also use Finder make alias a 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.

    0 讨论(0)
  • 2020-12-23 10:21

    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.

    ln works by creating a reference in the file system from one thing to another.

    The link you provide above suggests that ln will not work with the version of Python provided in MacPorts.

    0 讨论(0)
  • 2020-12-23 10:22

    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.

    0 讨论(0)
提交回复
热议问题