Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When using file to check some file types, I noticed that it seems to incorrectly label C++ files as C files:

$ file User.*
User.cpp: C source, ASCII text
User.h:   C++ source, ASCII text
User.o:   ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

Why is it correctly identifying the header file, but not the cpp file?

share|improve this question
up vote 9 down vote accepted

From man page of file command,

file command actually performs 3 tests on determining the file type.

First test

The filesystem tests are based on examining the return from a stat(2) system call.

Second test

The magic number tests are used to check for files with data in particular fixed formats.

Third test

The language tests look for particular strings (cf names.h) that can appear anywhere in the first few blocks of a file. For example, the keyword .br indicates that the file is most likely a troff(1) input file, just as the keyword struct indicates a C program.

The output of the file command is generally based on the result of any of the tests that succeeds.

Now, assuming the C++ program starts like this, and the third test succeeds,

#include <iostream.h>

As per the third test the keyword #include particularly specifies it is of type C program though we have a CPP program in hand. Now, when I check,

$ file example.cpp

example.cpp: ASCII C program text

Now, the concepts of object oriented are specific to C++. Let us create a file specific to C++.

I start my C++ program as,

Class something

Now, when I issue

$ file example.cpp

The output is,

example.cpp: ASCII C++ program text
share|improve this answer
Of key note is the purpose of file is specific to identifying files by content alone. – Joshua Jul 1 '14 at 23:05

Your Answer


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.