Getting the parent of a directory in Bash

前端 未结 12 1036
忘了有多久
忘了有多久 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:25

    ...but what is "seen here" is broken. Here's the fix:

    > pwd
    /home/me
    > x='Om Namah Shivaya'
    > mkdir "$x" && cd "$x"
    /home/me/Om Namah Shivaya
    > parentdir="$(dirname "$(pwd)")"
    > echo $parentdir
    /home/me
    

提交回复
热议问题