Unix Shell Command to extract portion of a string

I have a directory name in a string and I need to extract all except the last 2 subdirectories.  For example:

Starting String:

/abc/def/ghi/jkl/mno/

I need to get:
/abc/def/ghi

The number of levels of the directory is variable, but I always need to chop off the last two subdirectories from the string.  

I would like to do this in a single command.
rip55jcpAsked:
Who is Participating?
 
woolmilkporcConnect With a Mentor Commented:
dir=/abc/def/ghi/jkl/mno/
stripped=$(dirname $(dirname $dir))

echo $stripped
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.