LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-11-2009, 08:02 AM   #1
int80
LQ Newbie
 
Registered: Nov 2007
Posts: 19

Rep: Reputation: 0
Bash scripting -- parsing a directory path


Hi guys, I need to get the base path from a full path to a file.

/base/file/file.tar

I want to get "/base" from that. I know how to get the file and /base/file using basename and dirname and also that using sed, but I can't get the base dir.

Any help would be great.
 
Old 08-11-2009, 08:09 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Code:
echo /base/file/file.tar | cut -d "/" -f2
awk:
Code:
 echo /base/file/file.tar | awk -F"/" '{print "/"$2}'

Last edited by PMP; 08-11-2009 at 08:11 AM. Reason: awk based sol
 
Old 08-11-2009, 08:27 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
You are using "base" in 2 different ways, which is confusing.

Let's see if I understand correctly......Suppose you have "dir=/home/user/documents/myfile"

basename $dir gives: myfile

dirname $dir gives: /home/user/documents

You know how to do either one of these with SED, but you want to know how to get just "/home"

Try this:
Code:
echo $dir|sed 's%\(/[^/]*\).*%\1%'
If I got the question correct and/or you want the above decoded, please ask!!
 
Old 08-11-2009, 08:31 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
OK, so maybe SED is not the best for everything.......

In the "cut" and "awk" examples, why field #2?
 
Old 08-11-2009, 08:40 AM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,149

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Quote:
Originally Posted by pixellany View Post
OK, so maybe SED is not the best for everything.......

In the "cut" and "awk" examples, why field #2?
Because it (the directory name) precedes the second /, so it's the second field. (The empty string is a valid field value.)
 
Old 08-11-2009, 08:46 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by PTrenholme View Post
Because it (the directory name) precedes the second /, so it's the second field. (The empty string is a valid field value.)
Obvious when you think about it---you don't need a field separator until you have a field......

Confusion opportunity:
Code:
 big dog bark   ## dog is the 3rd field
big dog bark    ## dog is the 2nd field
Maybe SED was better.......

Last edited by pixellany; 08-11-2009 at 08:47 AM. Reason: Thought of something else clever to say
 
Old 08-11-2009, 09:26 AM   #7
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
:-)
 
Old 08-11-2009, 09:43 AM   #8
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware
Posts: 298

Rep: Reputation: 50
Quote:
Originally Posted by int80 View Post
Hi guys, I need to get the base path from a full path to a file.

/base/file/file.tar

I want to get "/base" from that. I know how to get the file and /base/file using basename and dirname and also that using sed, but I can't get the base dir.

Any help would be great.
foo=/base/file/file.tar

echo ${foo%/*} | sed 's?^\(/[^/]*\)/.*$?\1?'

Actually if all you need is the first top directory, then
echo $foo | sed 's?^\(/[^/]*\)/.*$?\1?'
would do it
(${foo%/*} would give /base/file)

Last edited by vonbiber; 08-11-2009 at 10:00 AM.
 
Old 08-11-2009, 10:06 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,541
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Code:
fullpath='/base/file/file.tar'
IFS='/' array=($fullpath)
echo "/${array[1]}"
That runs faster than using awk, cut or sed because no sub-process is required.
 
Old 08-11-2009, 10:38 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by pixellany View Post
Maybe SED was better.......
nah , don't think so.
 
Old 08-11-2009, 12:32 PM   #11
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,149

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
For what it's worth, here's a script that does it entirely in bash. With the "added value" that, by un-commenting the the commented lines, you get an array of the entire path. (If you're wondering, I wasn't familiar with the dirname and basname functions, so I was playing around.)
Code:
#!/bin/bash
topdir="$1"
name="$(dirname "$topdir")"
#i=0
while [ "$topdir" != "/" ];do
#  i=$(($i+1))
#  parse[$i]="$(basename "$topdir")"
  name="$topdir"
  topdir="$(dirname "$topdir")"
  [ "$topdir" = "." ] && topdir="$(pwd -P)"
done
echo "$name"
#while [ $i -gt 0 ]; do
#  echo -n " / ${parse[$i]}"
#  i=$(($i-1))
#done
#echo
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting directory names from a given path in Bash? ojha_riddhish Programming 10 04-13-2009 05:08 AM
Bash: Parsing Part of Directory Location guest Linux - Server 8 04-07-2009 12:13 AM
parsing a directory path using perl bharatbsharma Programming 4 12-08-2007 06:02 PM
Wrong parsing of ls command in bash scripting itz2000 Programming 3 04-25-2007 10:23 AM
Bash scripting and trying to determine whether a directory exists? obelxi Programming 9 04-18-2005 11:22 PM


All times are GMT -5. The time now is 03:22 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration