
The command basename is often used to extract the real file name without the file type specific file extension:
$ basename thisfile.txt .txt thisfile
Now sometimes you need it the other way around, you might want to have the file extension. There are of course hundreds of ways to do so, but I found this one appealing since it also shows how the command awk works (which I should learn a bit better I think):
$ echo "thisfile.txt"|awk -F . '{print $NF}'
txt
The “-F” marks the delimiter, “$NF” means the last field generated. Seems to be a pretty straightforward tool.
On a total unrelated note I really like the source code post style of WordPress.

September 29, 2007 at 22:34
Since you are usually operating on variables there are much simpler ways, without calling external tools:
> F=”thisfile.txt”
> echo ${F#*.}
txt
> echo ${F%.*}
jaki
September 29, 2007 at 22:41
Nice one as well, didn’t know that. But as I said there are hundreds of ways I think.
September 29, 2007 at 22:45
Using bash there’s also ${file%.*} to get the filename without the extension and ${file##*.} to get the extension alone. I.e.
file=”thisfile.txt”
echo “filename: ${file%.*}”
echo “extension: ${file##*.}”
outputs:
filename: thisfile
extension: txt
September 29, 2007 at 22:46
Well… almost what mikmach says…
September 30, 2007 at 11:42
The source code paste looks horrible in akregator: newlines are missing.
September 30, 2007 at 13:52
Thiago, I wonder if that is a bug in Akregator or in the source code thing…
September 30, 2007 at 18:08
I find the ‘basename’ command invaluable for simple batch renaming tasks such as:
#br
for file in *.$1; do
mv $file `basename $file $1`.$2
done
sh$ br htm html
I miss it the most on windows. Any simpler way to do such batch renames using bash/awk?
September 30, 2007 at 20:22
cool
October 1, 2007 at 12:42
sure, there hundred of ways.
But using awk is just overkill.
consider:
# echo foo.bar | cut -d . -f 2
Unlike the ${}-substitution, this works with non-bash sh-shells, too.
October 1, 2007 at 13:19
But …
$ echo “thisfile”|awk -F . ‘{print $NF}’
returns ‘thisfile’ so use
$ echo “thisfile.txt”|awk -F . ‘{if (NF>1) {print $NF}}’
which will return either the extension or null if there isn’t one.
October 2, 2007 at 10:57
another bash shortcut:
$ a = textfile.test.txt
$ echo ${a/*./}
txt
October 2, 2007 at 23:52
I never realized that there are so many ways to do that
June 9, 2008 at 6:14
Thanks Tommaso!
so much better than the gawk method!
I knew how to do it with gawk but really wanted another way.
July 1, 2008 at 3:33
… there is also sed for this:
echo “/real path/.to/file v1.2.3.extension” | sed ’s/.*\.//’
Note: this will also work with whitespace and multiple dots in filenames and path (the “cut”-method doesnt provide that feature). This only depends on #!/bin/sh, echo and sed.
July 1, 2008 at 3:45
replace single and double quotes with the corect ones if you c&p this…
January 18, 2009 at 16:09
[...] naam=’bestand.ext’ > echo ${naam#*.} Ah, net teruggevonden, bovenstaande en nog veel meer: http://liquidat.wordpress.com/2007/0…-shell-script/ Richard Rasker — [...]