Find Out What A File Is

Today we’ll have a simple article, one that shouldn’t take too long, as we learn one way to find out what a file is. It’s a handy command, but it won’t take all that long to explain. So, we’ve got that going for us!

The command we’re going to use is one that you almost certainly have installed by default. That command is the ‘file’ command. The man page describes file as:

file — determine file type

Linux pretty much treats everything like a file, pretty much. Files also contain a ‘magic number‘ – which is why you can rename an mp3 file as a gif file and it’ll still open in your media player. Basically, the magic number is the opening two bytes of a file that tell your system what kind of file you should be expecting. 

If you change the file extension it won’t fool the ‘file’ command. You can rename it with any extension you want and ‘file’ will know what the file is. Of course, it might be a text file, a compressed file, a Debian installer, etc… There are all sorts of file types out there. There are hundreds of file types just for image files.

So, yeah… We’ll be using the file command in this exercise. Let’s get to it!

Find Out What A File Is:

Like oh so many other articles, this one requires an open terminal. We do a great deal in the terminal. You can press CTRL + ALT + T and your default terminal should open.

With your terminal open, you’re probably in your home directory. So, the vast majority of you should be able to run the following command:

That file contains a bunch of the settings you see affected in the terminal. As you can see from the output, it’s an ASCII file, meaning you’d edit it with an ASCII (plain text) editor.

If you want, you can navigate to your Downloads directory and use ls to list the files. From there, you can run the file command on files you’ve downloaded. For example, I could run:

The output from that command would look like this:

The file command knows what the file is. It’s pretty accurate. It doesn’t matter if I change the .deb to .gif. 

See? Renaming the file didn’t fool the system. However, it might fool some applications. For example, if you click on that “.gif” file, your system might try to open it as an image and throw some sort of error. The file command can be pretty handy in those situations, where you’re not sure what the file type is and how you should approach it.

Closure:

Well, if you ever wanted to find out what a file is, you now know how. Over your computing journey, you may very well find you need to rely on the file command to know what the file type is. It’s a pretty handy command for when you need it. Of course, you could go so far as to edit the magic numbers to fool the command, but you’d probably know if you did that.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

How To: Find The File Type In The Linux Terminal

There are many file types in Linux, and we’ll learn to find the file type in the Linux terminal in this article. It’s not terribly difficult and is a good basic article, with a command not that widely discussed. It’s a good time to learn.

Let’s start at the beginning… As many of my readers are new, there’s some stuff you ought to know.

First things first, everything in Linux is a file. I realize that that may confuse some folks new to Unix/Linux, but it’s true. If you don’t know how this works, click this link. That should explain it well enough.

Linux also uses the whole Magic Bytes thing. You can click here and learn about Magic Bytes. To explain it a bit differently than Wikipedia – it’s why you can make a text file without an extension and still have it open with a text editor when you click on it. The system sets Magic Bytes that mark the file as being of a certain type.

Well, there’s a tool that you can use in the terminal to find a file type. Amazingly enough, that tool is called ‘file’. The man page for which is clear:

file — determine file type

Yup, that’s the tool and that’s what it does. It’s pretty accurate and works with a number of file types. It checks things like whether the file is an empty file, what response it sends when queried, if it has Magic Bytes, and the language used in the file. It’s pretty comprehensive.

Find The File Type:

Obviously, this requires an open terminal. After all, we’re finding the file type in the terminal. That kinda needs an open terminal! Just press CTRL + ALT + T and your default terminal should open. Tada!

Now, with your terminal open, and enter the following command. I’m pretty sure this will work on any desktop Linux!

It’ll happily spit out that you have a data file on your hands.

But, here, let’s see if we can fool it. Grab an image and call it image.png (or whatever extension) and run file <filename.extension> to see the output. Now, rename the file to just plain ‘image’ and run the command again (sans extension). What does it tell you? It should look something like this:

file command in action
See? It still knows that it’s an image file. Images use the Magic Bytes.

Go ahead and try to fool it. Rename it image.txt and try it again. Pretty neat, huh?

I don’t need to patronize, by now you get the idea. You see what it can do. Well, there’s a bit more. You can create a text file with a list of files in it (with their path if in different directory) and the run file on that file you created – just make sure it’s a plain text file that you created. It’ll happily output the types of all the files listed inside.

You can also use the whole wildcard thing. You can get all the file types in a directory with this command:

If you want all the files starting with the letter I, you’d do this:

If you want, you can even use it on compressed files. For that, there’s the -z flag. It looks something like this:

It’ll spit out some information, perhaps letting you know the minimum version of your archive manager needed to open it. It doesn’t give you information about the compressed files, however. To do that, you’d have to extract the files first.

Closure:

And there you have it. You have yet another article! This one shows you how to find the file type in the terminal, and is a handy tool indeed. I normally take the entirety of January off, but I can’t do that this year. This year, I must ensure there are articles. Maybe next year! The good news is I can author these things with a wee bit o’ the wine in me. So, there’s that!

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Subscribe To Our Newsletter
Get notified when new articles are published! It's free and I won't send you any spam.
Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.