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

2019-01-31 07:48发布

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 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?

7条回答
我命由我不由天
2楼-- · 2019-01-31 08:08

ln -s creates a symbolic link, which is almost a file on your filesystem

alias is a shell specific thing

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.

查看更多
看我几分像从前
3楼-- · 2019-01-31 08:10

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.

查看更多
疯言疯语
4楼-- · 2019-01-31 08:13

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.

查看更多
戒情不戒烟
5楼-- · 2019-01-31 08:13

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/

查看更多
6楼-- · 2019-01-31 08:19

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.

查看更多
在下西门庆
7楼-- · 2019-01-31 08:19

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.

查看更多
登录 后发表回答