5

I'm exploring compiling/running simple C++ programs from the bash shell (in Ubuntu 12.04). Being new to Linux in general, I actually have a couple questions.

First, it seems that Ubuntu/Linux does not display file types in the .abc format, as Windows does. I get that the OS doesn't need that extension to know the file's type, but isn't it more convenient for the human user to be able to read off that information at a glance? How am I supposed to know what type a given file is?

Second (and relatedly), if Linux seems not to care about extending a file name with the type information, why is that when I create a simple C++ program in gedit and just call it 'test', the compilation doesn't work (using the g++ compiler btw), but when I name the file 'test.cpp', it does work?

Does Linux need/care about the extension, or not? I feel like I'm getting mixed messages here, and I would really like to understand.

Braiam
  • 67,791
  • 32
  • 179
  • 269
  • Rants about your frustrations with file names and MIME types are not questions. Beyond that, this is a general programming question and would be better on another stack exchange site. – dobey Dec 12 '13 at 23:54
  • 6
    @dobey This is clearly a relevant question about how Ubuntu handles files in general and extensions. Perhaps you should read the question next time. – Paul Tanzini Dec 13 '13 at 00:13
  • See also: http://askubuntu.com/questions/803434/do-file-extensions-have-any-purpose-for-the-operating-system?noredirect=1&lq=1 – IMSoP Jul 28 '16 at 17:23

4 Answers4

5

You don't have to name your file with a particular suffix, however the gcc/g++ compilers use the suffix as a shortcut to decide how to process the files. If you want to use a different suffix (or no suffix) you can tell the compiler what kind of file it is explicitly using the -x command line option - see the answer to this similar previous question Compiling C source file without .c suffic

steeldriver
  • 136,215
  • 21
  • 243
  • 336
3

In any operating system, what you said is correct: a file's extension does not determine the type of file. Extensions are used to tell programs (and humans) what to expect the file's type to be. Generally, files with no extension are binaries, however, that is not a strict rule.

Does it matter if a file has an extension? It depends on the program. Some, like OpenOffice or Gedit, will not ask any questions about the file name extension, but will open the file anyway regardless of what error may occur. Smarter programs like g++ will examine the file name extension before parsing to prevent human error. And then there are programs like media players that trust you to use the correct extension for a file and use the extension to determine how to run.

In conclusion you could say that Linux doesn't care very much about file name extensions. This leaves it up to humans and programs to make educated decisions about naming their files. Extensions have their place, but they are not the determinant on what a file is.

Richard
  • 8,502
  • 11
  • 47
  • 72
2

human user to be able to read off that information at a glance?

Remember that Linux is an Unix-like operative system. Since ancient times *nix used different methods to identify what a file is about, like file, the shebang, the magic bit, etc. UNIX/Linux development as DOS, CP/M that Windows heritage does. So extensions are generally less significant to most Linux utilities and tools.

How am I supposed to know what type a given file is?

You ask the system to tell you. For that it's file and most (if not all) File Managers use the magic bit to identify what a file is about.

if Linux seems not to care about extending a file name with the type information, why is that when I create a simple C++ program in gedit and just call it 'test', the compilation doesn't work (using the g++ compiler btw), but when I name the file 'test.cpp', it does work?

This is for portability and since they were like that from the beggining. Since Linux doesn't care about the extension, but Windows does, then they make that both OS recognize what the file is about.

Braiam
  • 67,791
  • 32
  • 179
  • 269
  • That makes sense, since the g++ compiler needs to work on both OS's. I.e. my specific issue with compiling the C++ code is not a Linux issue but a g++ issue. – ghostRepeater Dec 13 '13 at 19:29
  • @ghostRepeater exactly, you g++/c++ can be independent of the OS you use, also, C was way before *nix came around. ;) – Braiam Dec 13 '13 at 19:32
1

Unix was created with the idea of files, descriptors, permissions, pipes, links and programs etc interacting with each other. It was assumed that the programmers would do it right. So the whole linux branch of Unix is built on the same idea, not strict type extensions used as a basis for system organisation. It just happens to be the case that the early c compilers that were used to build unix had file extensions to make interaction with object, source and compiled files easier to use. So they've stuck.

Windows doesn't use type extensions to help human users, but because the structures it uses are far less flexible.

Finally, gnu/linux/ubuntu are also learning experiences of necessity and you should have a positive attitude to the differences between them and Windows, because you are really going to learn how computers work by doing that.

comrademike
  • 784
  • 1
  • 9
  • 14
  • "Windows doesn't use type extensions to help human users, but because the structures it uses are far less flexible." Really? What flexibility does Windows (or, more relevantly, MS-DOS) lack that file extensions solve? – IMSoP Aug 26 '20 at 10:55