Getting the parent of a directory in Bash

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

    Depending on whether you need absolute paths you may want to take an extra step:

    child='/home/smith/Desktop/Test/'
    parent=$(dirname "$child")
    abs_parent=$(realpath "$parent")
    

提交回复
热议问题