Get extension of file

Asked by hacktolive

Can anyone give me a way to get a file extension?
Assuming the filename is $FILENAME

I have this method:
> FILENAME=example.txt
> echo ${FILENAME#*.}

But is does not work if the file as no extension. (if gives just gives me the name of the file)

Thanks

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
actionparsnip (andrew-woodhead666) said :
#1

you could first check the filename contains a '.' character, this would usually indicate a file extension

In Linux the file extension doesnt mean a right lot as the format of the data can be interpretted by the command:

file

regardless of extension.

for example:

andy@fileserver:~$ wget http://t.dnlr.net/articles/img/small-tux.jpg
--2009-04-22 09:40:29-- http://t.dnlr.net/articles/img/small-tux.jpg
Resolving t.dnlr.net... 216.234.124.190
Connecting to t.dnlr.net|216.234.124.190|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1987 (1.9K) [image/jpeg]
Saving to: `small-tux.jpg'

100%[======================================>] 1,987 --.-K/s in 0.1s

2009-04-22 09:40:30 (16.5 KB/s) - `small-tux.jpg' saved [1987/1987]

andy@fileserver:~$ ls
Documents Downloads Music Pictures small-tux.jpg Videos
andy@fileserver:~$ file ./small-tux.jpg
./small-tux.jpg: JPEG image data, JFIF standard 1.01, comment: "CREATOR: gd-jpeg v1.0 (using IJ"
andy@fileserver:~$ mv ./small-tux.jpg ./small-tux.png
andy@fileserver:~$ file ./small-tux.png
./small-tux.png: JPEG image data, JFIF standard 1.01, comment: "CREATOR: gd-jpeg v1.0 (using IJ"
andy@fileserver:~$

the extension may now be .png but the system still KNOWS it is a JPG due to te way the data is forrmed.

What are you trying to achieve? There may already be a solution...

Revision history for this message
hacktolive (hacktolive-org) said :
#2

Yes, Yes, I know about the "Linux magic"...
What I wanted to do was to detect if the file is a .desktop file, and if it is, give a message in App Runner ( http://hacktolive.org/runner ) to just double click the file

Revision history for this message
Andre Mangan (kyphi) said :
#3

I am not at all clear as to what you are looking for, hacktolive.

Do you mean the file utility? (man file utility).

Via the GUI you can just right click a file, go to Properties and it will tell you what type of file it is.

Can you help with this problem?

Provide an answer of your own, or ask hacktolive for more information if necessary.

To post a message you must log in.