Remove part of path on Unix

前端 未结 7 1169
滥情空心
滥情空心 2020-11-30 21:56

I\'m trying to remove part of the path in a string. I have the path:

/path/to/file/drive/file/path/

I want to remove the first part /

7条回答
  •  独厮守ぢ
    2020-11-30 22:27

    If you want to remove the first N parts of the path, you could of course use N calls to dirname, as in glenn's answer, but it's probably easier to use globbing:

    path=/path/to/file/drive/file/path/
    echo "${path#*/*/*/*/*/}"   #  file/path/
    

    Specifically, ${path#*/*/*/*/*/} means "return $path minus the shortest prefix that contains 5 slashes".

提交回复
热议问题