readlink -f allows the final path element to be a broken symlink, or simply non-existent. ![]() There is a small difference in functionality between the canonpath function above and GNU's readlink -f, in that canonpath explicitly requires that the path must exist. # can also compare against `greadlink -f` if you have installed it with homebrew Testing examples: # use `set -vx` if you want to see the gory details of unwinding the paths _canon_dir() ( builtin cd -P "$1" &>/dev/null & pwd ) # - target may be symlink, file or directory path may contain. # Print absolute canonical path, like GNU's `readlink -f` A recent update to macOS (12.3) has apparently introduced readlink -f natively as well.ĭeveloping a solution in Bash to do the job across more platforms is a tricky problem, but the following canonpath function correctly handles the edge cases that cause some simpler solutions to fail: canonpath() ( There are other answers using a python one-liner, or suggesting to install GNU programs using homebrew so you can have access to readlink -f or realpath. With find, I don't even know what you're trying to do. So it's useless for a regular file, and for a directory you need to change into it first. pwd only prints information about the current directory. Pwd -P can work as well, but only for a (symlink to a) directory. Or (putting the history modifier directly on the parameter expansion) zsh -c 'echo $0:A' /usr/local/bin/python3 echo =python3(:A)Įven if you aren't running zsh, you can invoke it just for that. If this is the python3 in your $PATH, you can save typing with equals expansion. The history modifier A turns the given path into an absolute path and resolves all symbolic links. A colon : in the list of glob qualifiers starts a list of history modifiers. it's equivalent to GNU readlink -f, not to a plain readlink).Įxplanation: the parentheses (…) at the end of a word surround a list of glob qualifiers. This prints the absolute path to the file with all symbolic links resolved (i.e. Or more robustly, in case there are backslashes in the path: print -lr - /usr/local/bin/python3(:A) They just print the symbolic links path for me: FantasticMrFox:bin robert$ pwd -P python3įantasticMrFox:bin robert$ find `pwd -P` -name python3 outputting the full path to the symbolic link). The problem is that these don't seem to work on OS X the way that people describe them working on linux (i.e. I've found some tidbits here and there regarding use of pwd -P and the find utility to do this. ![]() However, I can't seem to get any of the command line tools to resolve that path and display the actual path to python3. /./Library/Frameworks/amework/Versions/3.1/bin/python3 If I do ls -lhaG I see -> and the relative path to the right of the symlinks: lrwxr-xr-x 1 root wheel 69B Dec 7 22:29 python3 ->. There's some weird behavior surrounding this. I was looking at the executables in /usr/local/bin and they are all symbolic links to. I have a tangled mess of python installations on my laptop.
0 Comments
Leave a Reply. |