Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to create a file called "message" that contains text only. How does the system know what program to use to read it if I decide to call it:

  • message.txt?
  • message.dat?
  • message.enc?
  • message.cpz?
  • message.asdfasdf?
  • message.abcdefghijklmnopqrstuvwxyz?
  • message.this_is_so_random?
  • or just 'message' with no extension?

I was wondering this for text-only files, but what about files that contain video and audio?

share|improve this question

migrated from askubuntu.com Sep 18 '11 at 1:11

This question came from our site for Ubuntu users and developers.

4 Answers 4

up vote 7 down vote accepted

Linux (/Ubuntu) does not depend on the file extension as Windows does. Every file has a part inside it where it identifies itself as to what it is. To show what a random file is we have the command file. Some random examples:

$ file *
Aptana Studio 3:       directory
Create ubuntu live cd: ASCII English text, with very long lines
1.txt:      empty
gedit_open: Bourne-Again shell script text executable

You can also find the mimetype from command-line:

$ file --mime-type -b 1.txt 
application/x-empty
$ file --mime-type -b gedit_open 
text/x-shellscript

We do have a ~/.local/share/applications/mimeapps.list where you can associate programs to the extension. You can set those with the open with inside properties of a file when using Nautilus or by using command-line (see How to add a MIME type with a project created in Quickly? )

So basically you can do whatever you want with a filename but I would suggest to use for instance .conf for it if it contains configuration settings but this is strictly for human understanding. Since you generally need to execute a file for it to be associated with the program that is set for this mimetype and you generally set the permission to non executable it is not a issue.

share|improve this answer
1  
Most file managers do use extensions, especially when dealing with files using very common container formats. For example, OOXML, Java JAR, and many other formats are just standard Zip archives which (except ODT) have no strictly defined magic. –  grawity Sep 30 '11 at 8:47

Generally yes, you can use whatever extension you want, also there's no need to, say, register file extensions through some international body or anything.

However, some programs expect their files to have a certain extension - this usually matters more in a graphical environment - i.e. Open File dialogs filter their contents by extension, or Save File dialog can guess in what format to stave the file according to which extension user provided.

In Windows using arbitrary extensions may be e bit more troublesome because of the way Open and Save dialogs work, but still you can have a text file with extension .kjhkhkj

Regarding "how the system decides what program to use": you tell it. In the terminal you call the program and give it a file name:

gedit message.kjhkjhkj

and it'll be open in gedit

In Nautilus, you right-click on the file and choose "Open with...", where you can specify a program. It's also possible to make this association permanent so .kjhkhkj files are always opened with gedit.

In Windows the procedure is similar, though Windows tend to rely more on known file extensions (i.e. video files will have preview thumbnails in Ubuntu regardless of their extension, Windows need them to have one of known extensions (.avi, .mov etc.)

share|improve this answer

Yes and no. On windows the filetype extention is used to determine what opens a file. if you rename all your mp3s .tomato, windows wouldn't know what to do with it unless you assigned a programme that can open mp3s as the default for opening .tomato or you specifically opened the file with it.

*nixes use magic numbers for the same purpose, so they don't really care about the extension

share|improve this answer

Also some applications, like grep, that handle files differently try to guess what the contents are because they can handle differently based on that. My sample of grep ignores the extension (I had it think a plaintext .log was binary) but some applications (eg Nautilus) will prefer to use an extension for the guess.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.