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.
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 usrYou 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 filelistIf 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.
Related Shell Script Programming articles:
✔ Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...✔ Parsing "id" strings in a Shell Script?
Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...✔ Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...✔ Script to test line lengths for Twitter compatibility?
I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...✔ Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
I do have a comment, now that you mention it!
Check This Out Too...
Look for Answers
All Our Categories
Apple iPad Help
Articles and Reviews
Auctions and Online Shopping
Blogs and Blogging
Building Web Site Traffic
Business and Management
Computer and Internet Basics
d) None of the Above
Google Gmail Help
Google Plus Help
Industry News and Trade Shows
iPhone and Cell Phone Help
iPod, Sony PSP and MP3 Player Help
Kindle Fire Help
Mac OS X Help
Pay Per Click (PPC) Advertising
Search Engine Optimization (SEO)
Shell Script Programming
Tech Support Video Help
The Writing Business
Twitter, LinkedIn and Social Network Help
Unix and Linux Help
Video Game Tips and Help
Windows PC Help
Find Me on Google+
ADT on G+