views:

56

answers:

2

How would I get just the filename without the extension and no path?

The following gives me no extension but I still have the path attached:

source_file_filename_no_ext=${source_file%.*}
+1  A: 

Most UNIXes have a basename executable for just that purpose.

pax> a=/tmp/file.txt
pax> b=$(basename $a)
pax> echo $b
file.txt

If you want a bash-only solution, you can start with:

pax> a=/tmp/xx/file.tar.gz
pax> xpath=${a%/*} 
pax> xbase=${a##*/}
pax> xfext=${xbase##*.}
pax> xpref=${xbase%.*}
pax> echo;echo path=${xpath};echo pref=${xpref};echo ext=${xfext}

path=/tmp/xx
pref=file.tar
ext=gz

That little snippet sets xpath (the file path), xpref (the file prefix) and xfext (the file extension).

paxdiablo
I know there is something to do with bash like the above. I just don't know what the key word is. I would like to get get the pathname, filename, and extension separated into different variables.
Keith
If you want to get path use:path=$(echo $filename | sed -e 's/\/[^\/]*$/\//')If you want to get extension:ext=$(echo $filename | sed -e 's/[^\.]*\.//')
jcubic
@Keith: for pathname, use `path=$(dirname $filename)`; there isn't a command to give you the extension per se, but @paxdiablo showed you how the shell can do it.
Jonathan Leffler
A: 
$ source_file_filename_no_ext=${source_file%.*}
$ echo ${source_file_filename_no_ext##*/}
ghostdog74