Getting the parent of a directory in Bash

前端 未结 12 1055
忘了有多久
忘了有多久 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条回答
  •  旧时难觅i
    2020-12-04 06:34

    dir=/home/smith/Desktop/Test
    parentdir="$(dirname "$dir")"
    

    Works if there is a trailing slash, too.

提交回复
热议问题