LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 533

Rep: Reputation: 129Reputation: 129
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: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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
 
Old 05-27-2017, 09:53 AM   #12
C. Pappy
LQ Newbie
 
Registered: May 2017
Posts: 1

Rep: Reputation: Disabled
I recognize that this is a very old thread, but I think that the following solution could be generally useful because it demonstrates the power of bash pattern matching.


Code:
> cat t
[[ "$1" =~ ((/|)[^/]*)(/|) ]]
echo ${BASH_REMATCH[1]}
> t /base/file/file.tar
/base
> t base/file/file.tar
base
> t /base
/base
> t base
base
> t

>
 
Old 05-27-2017, 11:15 AM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Well if you know what you're working with in directory depth
Code:
userx%voider ⚡ testing ⚡> firstDir="/base/file/file.tar"
userx%voider ⚡ testing ⚡> BaseDir=${firstDir%/*/*}
userx%voider ⚡ testing ⚡> echo $BaseDir
/base
 
Old 05-27-2017, 11:17 PM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Note: from '/base/file/file.tar' part 'file' might very well be a symlink to '/somewhere/else'
 
1 members found this post helpful.
Old 05-27-2017, 11:33 PM   #15
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Welcome to LQ!

Quote:
Originally Posted by C. Pappy View Post
I recognize that this is a very old thread...
Let's let old threads sleep peacefully, please.

If you have a specific question or your own use case similar to one in an inactive thread, please start your own thread and include the details of your own case. This prevents confusion that can arise from similar but not exactly the same constraints, and the resulting discussion necessary to discover and redefine those constraints.
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:01 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration