Short Tip: Get file extension in Shell script

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.

Blog Stats

Google Plus

License – CC-BY-SA

Beware however that this refers only to parts which are obviously written by me and do not have any other information about licensing. Quoted text, pictures and other content created by others is copyrighted by the corresponding authors. If you are in doubt, ask before republishing any content.