Imagine a path like this: /a/b/c/d/e/...
/a/b/c/d/e/...
Where ... could be any number of levels further deep, i.e., I don\'t know, ahead of time, whether ther
...
You can use cut:
cut
s='/a/b/c/d/e/f/g/h/i/j/k' echo "$s" | cut -d/ -f1-5 /a/b/c/d
Or if you are using BASH then you can use shell array:
BASH
IFS=/ arr=($s)
Then print desired elements from array:
IFS=/ echo "${arr[*]:0:5}" /a/b/c/d