Search code examples
bashmacos-sierra

MacOS get directory symlink'd file is located in


Is it possible to get the directory a symlink'd file is located in. For example:

File A in directory X is a symlink of file B in directory Y. I want the path to directory Y while running scripts involving file A.

Edit: utilizing readlink doesn't work same way in MacOS as it does in other Unix projects. Would prefer to not need to install additional tools.


Solution

  • Based on chepner's comments was able to get this to work:

    path=`dirname "$(readlink "$BASH_SOURCE")"`