6

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?

7

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 https://askubuntu.com/questions/46020/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.

1
  • 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.
    – user1686
    Sep 30 '11 at 8:47
4

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.)

3

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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