Solved

Unix Shell Command to extract portion of a string

Posted on 2013-01-14
1
561 Views
Last Modified: 2013-01-14
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
Comment
Question by:rip55jcp
1 Comment
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 38774455
dir=/abc/def/ghi/jkl/mno/
stripped=$(dirname $(dirname $dir))

echo $stripped
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now