Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 207
charltoons Avatar asked Aug 26 '13 20:08

charltoons


2 Answers

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.

like image 94
David W. Avatar answered Sep 28 '22 02:09

David W.


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.)

like image 32
Keith Thompson Avatar answered Sep 28 '22 03:09

Keith Thompson