I think that pwd -P
and readlink
are going to be your friends for this task.
"How can I get the behavior of GNU's readlink -f on a Mac?" is a handy resource.
pwd -P
only works if you're inside the symlink directory:
14:07:13 jason@mac ~ $ cd bin
14:08:08 jason@mac bin $ pwd -P
/Users/jason/Applications
readlink
works by specifying the target (thus it can be used against files):
14:09:03 jason@mac ~ $ readlink bin
Applications
14:09:34 jason@mac ~ $ readlink /var
private/var
The output of readlink
appears to be relative to the parent of the specified target.
Ex: The parent of /var
is /
, so private/var
is correct, relative to /
. Per my above example of bin -> Applications
, both are in my Home Directory, no matter where I run it, the output is the same.
Best Answer
An alias contains two pieces of information: a unique identifier of the file it links to, and the path and file name of the file it links to.
If you rename or move a file, and then create a new file with the path and file name that the file originally had, then any alias that linked to the original file now links to the new file.
However, if you rename or move a file without replacing it, and then invoke an alias, the alias is updated to point to the new path and file name, making use of the unique identifier to do so.
A symbolic link, on the other hand, does not contain a unique identifier to a file, and would appear as broken if the file is renamed or moved and not replaced with a file of the same path and file name.
Your choice should depend on which scenario suits you best.