General

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:

file ~/.bashrc

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:

file Yandex.deb

The output from that command would look like this:

$ file Yandex.deb
Yandex.deb: Debian binary package (format 2.0), with control.tar.gz, data compression xz

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

$ file Yandex.gif
Yandex.gif: Debian binary package (format 2.0), with control.tar.gz, data compression xz

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.

KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

Recent Posts

A Quick Look At The Shutdown Command

This is going to be just a quick article about a command many of you…

2 days ago

Setting Up Coding Environments on Linux for Educational Use

With so many strong attributes, such as robustness and flexibility, Linux stands as a powerful…

4 days ago

View Detailed Hardware Information

There are many tools for showing your hardware information and today we'll get to view…

6 days ago

How To: Install Wine In Lubuntu

Today's article isn't going to be complicated or long as we are going to cover…

1 week ago

Update Python Packages (PIP)

We've had a run of Python packages recently and you can tell that I'm a…

1 week ago

Save A Command’s Output To A File (While Showing It In The Terminal)

The title is the best I can come up with to describe this exercise as…

2 weeks ago