Register
It is currently Sat Sep 20, 2014 12:01 pm

Splitting filename from absolute path


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Thu May 10, 2007 1:11 am   

Joined: Thu May 10, 2007 12:36 am
Posts: 1
Hi,

I need the ability to take an absolute path and filename as a string and remove the filename segment from it leaving me with just the path to the filename.

I saw this illustrated on the website somewhere, but I lost the page in my browser history and i can't remember what i was googling for when i found it.

Is there a simple function in BASH scripting to let me do this?


Top
 Profile  
 PostPosted: Thu May 10, 2007 1:46 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Heya Catfish,

I know what you're talking about, and I'm trying to remember what it was... You can use "basename" to get the filename without the path, but I can't remember what(if) there was a built in command to get the directory without the file name. In the meantime, here's some fancy awk stuff to get what you're looking for:

Code:
#!/bin/bash
#test script for file/path manipulation

# First, we'll need a path, so let's make one up

fullpath="/mnt/test/this/is/a/ridiculously/long/path/to/test.file"


# Now we'll chop the filename off of the end.  In awk, NF
# is the Number of Fields.  If there are 10 fields, $NF is
# equal to $10, or whatever the last field is.  We're using
# the slash (/) as a field seperator (escaped with a "\"
# filename="$(echo "$fullpath" |awk -F\/ '{ print $NF }')"

# or using basename:
filename="$(basename "$fullpath")"


# Now I'm just being lazy and using sed to chop off the
# filename that we grabbed for our $filename variable
# (commenting this out, since finding 'dirname')
# use this if you still want the trailing slash :)

#directory="$(echo "$fullpath"|sed s/"$filename"/""/g)"


# The dirname command!  This works even if the file
# you give it doesn't actually exist.  It does chop off
# the trailing slash though, so watch out for that.
# Also, if you don't give it a full path, it will return
# a relative path.

directory="$(dirname "$fullpath")"


# Now for the output, in case anybody
# wants to run this script as-is

echo "$fullpath"
echo "$directory"
echo "$filename"


Hope this helps!

EDITED: Forgot comments :)


Last edited by jeo on Thu May 10, 2007 3:24 pm, edited 3 times in total.

Top
 Profile YIM  
 PostPosted: Thu May 10, 2007 3:14 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Other possibilities for directory include:

Code:
# using grep + regex:

directory="$(echo "$fullpath"|grep -Eo '\/.*\/')"


# using awk:  won't work if there are any spaces
# in directory names because all spaces get
# replaced with a "/"...

directory="$(echo "$fullpath"| awk -F\/ '{ $NF = ""; gsub(/\ /,"/");print }' )"

# found it! i looked at dirname when i was writing that first script
# but dismissed it because it was giving me relative paths.  If you use
# dirname and give it the full path, it chops off the filename and the
# trailing slash!  the file and path don't even have to exist!

directory="$(dirname "$fullpath")"


I think that just about covers all the methods I can think of. "dirname" is the command I was looking for originally, I had just forgotten how to use it. Editing original script ;)


Top
 Profile YIM  
 PostPosted: Thu May 10, 2007 3:25 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
I fail at forum posting... from now on I will post AFTER researching, not during... I must have edited the above posts like 5 times =P


Top
 Profile YIM  
 PostPosted: Fri May 11, 2007 11:12 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 667
Location: Des Moines, Iowa
:lol:

yeah.... I've did that before myself :)


Top
 Profile WWW  
 PostPosted: Thu Jun 28, 2007 12:07 am   

Joined: Wed Jun 27, 2007 11:07 pm
Posts: 2
Location: Colorado -- USA
You could also use "pure" Bash and avoid bloatware like 'dirname' :^)

Code:

FULL="/usr/local/bin/snark"

PARTIAL=${FULL%/*}

echo $PARTIAL

   /usr/local/bin


The Advanced Bash Scripting Guide's chapter on string manipulation has more info.

"Never pipe when Bash will do." -- Lao Tzu


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP