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
