How-To get root directory of given path in bash?

前端 未结 6 1617
你的背包
你的背包 2021-01-04 01:02

My script:

    #!/usr/bin/env bash
    PATH=/home/user/example/foo/bar
    mkdir -p /tmp/backup$PATH

And now I want to get first folder of

6条回答
  •  有刺的猬
    2021-01-04 01:20

    You can try this awk command:

     basedirectory=$(echo "$PATH" | awk -F "/" '{print $2}')
    

    At this point basedirectory will be the string home Then you write:

    rm -rf ./"$basedirectory"/
    

提交回复
热议问题