Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

File extensions are not necessary on unices, still every tarred, gzipped or bzipped file I encounter has a file extension like .tar, .tar.gz or .tgz.

Is there any special reason for that or is that just convention?

share|improve this question
add comment

3 Answers 3

up vote 13 down vote accepted

Originally, on unix systems, the extensions on file names were a matter of convention. They allowed a human being to choose the right program to open a file. The modern convention is to use extensions in most cases; common exceptions are:

  • Only regular files have an extension, not directories or device names. The mere fact of being a directory or device is enough file type indication.
  • Executables that are meant to be invoked directly don't have an extension. The mere fact of being executable is enough information for the user, and the kernel doesn't care about file names.
  • Files beginning with a word in all caps are often text files, e.g. README, TODO. Sometimes there is an additional part that indicate a subcategory, e.g. INSTALL.linux, INSTALL.solaris.
  • Files whose name begins with a dot are configuration or state files of a particular application, and often don't have an extension, e.g. .bashrc, .profile, .emacs.
  • There are a few traditional cases, e.g. Makefile.

(These are common cases, not hard-and-fast rules.)

Most binary file formats also contain some kind of header that describes properties of the file, and typically allows the file format to be identified through magic numbers. The file command looks at this information and shows you its guesses.

Sometimes the file extension gives more information than the file format, sometimes it's the other way round. For example many file formats consist of a zip archive: Java libraries (.jar), OpenOffice documents (.odt, …), Microsoft Office document (.docx, …), etc. Another example is source code files, where the extension indicates the programming language, which can be difficult for a computer to guess automatically from the file contents. Conversely, some extensions are wildly ambiguous, for example .o is used for compiled code files (object files), but inspection of the file contents usually easily reveals what machine type and operating system the object file is for.

An advantage of the extension is that it's a lot faster to recognize it than to open the file and look for magic sequences. For example completion of file names in shells is almost always based on the name (mainly the extension), because reading every file in a large directory can take a long time whereas just reading the file names is fast enough for a Tab press.

Sometimes changing a file's extension can allow you to say how a file is to be interpreted, when two file formats are almost, but not wholly identical. For example a web server might treat .shtml and .html differently, the former undergoing some server-side preprocessing, the latter being served as-is.

In the case of gzip archives, gzip won't recompress files whose name ends in .gz, .tgz and a few other extensions. That way you can run gzip * to compress every file in a directory, and already compressed files are not modified.

share|improve this answer
add comment

They may not need an extension, but it sure makes identifying them easier in the output of ls.

share|improve this answer
2  
Exactly. Usually there is not a "hold my hand please" flag to tar, so you have to tell it what kind of archive you've got. –  Kevin Cantu Nov 17 '10 at 20:17
add comment

File extensions are primarily a convention for the humans who use the system. There are tools which do use the filename extension to do things. For example Nautilus shows me a different icon based on the file extension.

If I gave you a file called file, you might not know how to open this file. However, if I gave you a file named file.tar.gz or file.tar you could quickly and easily figure it out.

share|improve this answer
3  
Nautilus uses more than just the file extension to determine file type. A tar.gz file without the extension will still usually get a fancy symbol. –  Steven D Nov 17 '10 at 19:03
add comment

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.