MangO_O MangO_O - 1 year ago 92
Bash Question

Retrieve parent directory of script

I'm working on an uninstaller script to delete the parent folder where the script is installed.


So has to do this:

rm- rf /usr/local/Myapplication

I can retrieve the folder where uninstall resides

SYMLINKS=$(readlink -f "$0")

But I'm still unsure of the pretty way to get the parent path.
I thought of using sed to demove the "Uninstaller" part of this path, but is there an elegant way to get the path to Myapplication folder to delete it?

Thank you

Answer Source

How about using dirname twice?

APP_ROOT="$(dirname "$(dirname "$(readlink "$0")")")"

The quoting desaster is only necessary to guard against whitespace in paths. Otherwise it would be more pleasing to the eye:

APP_ROOT=$(dirname $(dirname $(readlink $0)))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download