Absolute Paths From Relative Paths In Bash
There doesn’t seem to be a nice, cross-platform way of deriving the absolute path from a relative one in Bash.
(`readline -f .` works in Linux, but doesn’t seem to work in OS X).
Here is a small program written in Python which does exactly that:
#!/usr/bin/env python
import os, sys
if len(sys.argv) < 2:
print 'usage: %s <PATHS>' % sys.argv[0]
raise SystemExit(1)
directory = os.path.dirname(sys.argv[1])
if len(sys.argv) > 2:
directory = os.path.join(directory, *sys.argv[2:])
print os.path.abspath(directory)Paste it into a file and chmod a+x it and you have yourself a handy utility:
~/Projects $ abspath .
/Users/simon/Projects
~/Projects $ abspath ../
/Users/simonI use it in a script called server that starts various web-services. By starting the script with:
#!/bin/sh
directory=`abspath $0 ..`
cd $directory
...you can now run it from anywhere on the file system. The script will cd itself into the web service’s working directory before kicking things off.