Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, iphone help, ipod help, AdSense, MySpace, Sony PSP help, Mp3 players, Windows XP, Windows Vista, Linux, SEO, Mac OS X, Facebook, Twitter and LinkedIn.

Extracting Directory Names within a Shell Script

A reader writes:
First, thanks for writing Wicked Cool Shell Scripts. I've found them quite useful and I've learned many things from those scripts. I just have a question and I was wondering if you could point me in the right direction.

I'm trying to write a shell script that will pull the top-level directories out of a list of file paths, example:

list of files:
/usr/local/test
/usr/bin/test
/opt/Tivoli/dat
/opt/lib/test
/tmp/files/test
/tmp/local/test

Output after script:
/usr
/opt
/tmp


Dave's Answer: Initially I thought that this would be a perfect job for dirname, the partner to the better known basename utility. It's not, though, because basename /usr/lib/test is "/usr/lib", not just "/usr". So instead, I'd use cut, a utility I write about in the book too. The cut utility lets you specify the delimiter for field separation and also lets you specify what field you're interested in, so here's what happens when we put all of that together:
$ echo /usr/lib/test | cut -d/ -f2
usr
You can drop that into a pipeline and if we assume that your list of files is itself in a file called "filelist", you're halfway there with this one line:
cut -d/ -f2 filelist
If you want to prepend the slash, you can do that with sed, but let's tackle the "unique" part first with uniq. Since that particular utliity only works if the input is sorted, here's the pipe I have:
cut -d/ -f2 filelist | sort | uniq | sed 's/^/\//'
That'll do everything you want and it's short enough to be an alias!

Hope that helps and good luck with your script writing efforts.



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

Good question, I tried this way:

[jsaikia@zorro trap]$ cat dir.names
/usr/local/test
/usr/bin/test
/opt/Tivoli/dat
/opt/lib/test
/tmp/files/test
/tmp/local/test

[jsaikia@zorro trap]$ cat dir.names | awk -F "/" '{print "/"$2}' | sort | uniq
/opt
/tmp
/usr

Posted by: Jadu at February 26, 2007 10:15 PM

Hi, I have problem with some shell programing ,would you please help me?
1) I am trying to write a shell script in ksh to do like dir and shows directories
2)Is it possible to write a shell script in ksh with FOR to loop infinitely?
3)I want to write a shell script in ksh to mark every line(menu)that cursor is and show submenu.

thanks alot for your kindly attention

Posted by: leila at November 7, 2008 6:15 AM


I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Uniblue: Free Virus Scan

Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Member of the B5Media Network

Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]