Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to remove the part


from the full path


so that it results in


I've tried ${path#[!/]*/*} and ${path#/*/*} but both result in wrong paths.

share|improve this question
If you have received an acceptable answer, then please consider accepting it by clicking on the outline of a checkmark to the left of the answer. This will aid future users who have the same question. – Kevin M Apr 26 '10 at 18:26

this will work ${path%/*/*}, but works from the back to the front. here is a good how-to for chopping strings in bash.

share|improve this answer

If you could deal with /usr/share/man/man1, then you could do dirname /usr/share/man/man1/bmtoa.1.gz, but since you stated that you need the man1 to also come off, so you'll have to daisy-chain them:

dirname `dirname /usr/share/man1/bmtoa.1.gz`
share|improve this answer
It doesn't use shell expansion as requested by to me dirname looks much saner and simpler. – Jason Tan Apr 25 '10 at 15:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.