Getting the parent of a directory in Bash

前端 未结 12 1037
忘了有多久
忘了有多久 2020-12-04 06:11

If I have a file path such as...

/home/smith/Desktop/Test
/home/smith/Desktop/Test/

How do I change the string so it will be the parent dir

12条回答
  •  独厮守ぢ
    2020-12-04 06:26

    use this : export MYVAR="$(dirname "$(dirname "$(dirname "$(dirname $PWD)")")")" if you want 4th parent directory

    export MYVAR="$(dirname "$(dirname "$(dirname $PWD)")")" if you want 3rd parent directory

    export MYVAR="$(dirname "$(dirname $PWD)")" if you want 2nd parent directory

提交回复
热议问题