Many UNIX-like operating systems have a basename
executable for a very similar purpose (and dirname
for the path):
pax> full_name=/tmp/file.txt
pax> base_name=$(basename ${full_name})
pax> echo ${base_name}
file.txt
That unfortunately just gives you the file name, including the extension, so you’d need to find a way to strip that off as well.
So, given you have to do that anyway, you may as well find a method that can strip off the path and the extension.
One way to do that (and this is a bash
-only solution, needing no other executables):
pax> full_name=/tmp/xx/file.tar.gz
pax> xpath=${full_name%/*}
pax> xbase=${full_name##*/}
pax> xfext=${xbase##*.}
pax> xpref=${xbase%.*}
pax> echo "path="${xpath}", pref="${xpref}", ext="${xfext}""
path="/tmp/xx", pref="file.tar", ext="gz"
That little snippet sets xpath
(the file path), xpref
(the file prefix, what you were specifically asking for) and xfext
(the file extension).