• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 569
  • Last Modified:

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.
0
rip55jcp
Asked:
rip55jcp
1 Solution
 
woolmilkporcCommented:
dir=/abc/def/ghi/jkl/mno/
stripped=$(dirname $(dirname $dir))

echo $stripped
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now