Get the parent directory of a given file
Assuming
$ file=./Tools/build.bat
With a POSIX compatible shell (including zsh):
$ echo "${file%/*}"
./Tools
With dirname
:
$ echo "$(dirname -- "$file")"
./Tools
(at least GNU dirname
takes options, so the --
is required in case the path starts with a dash.)
If you are using zsh
try :h
modifier
cd $file:h
You can add n of them to go n levels up in directory structure.
this is quite simple with the command dirname, just do the folowing:
cd "$(dirname -- "$file")"
now you can even go further on this
file=/home/switch87/.bashrc
cd "$(dirname -- "$file")"
cd "$(dirname -- "$file")"/..
first cd will get you to /home/switch87, the seccond to /home