Learn how to use the Linux file command in this beginner-friendly tutorial! The file command is a powerful tool that helps you quickly identify file types on the Linux command line — whether it’s text, binary, image, or something else.
In this video, I’ll cover the basic usage of the file command, show you several practical examples, and explain how you can use it to understand what kind of files you’re working with in Linux. If you’re learning Linux or want to sharpen your command line skills, this tutorial will help you understand one of the most useful Linux commands.
LCC – The file Command
Basic Usage
So, let’s see some examples of the file command. And like I mentioned during the intro, it’s a simple command – so you’ll learn it fairly quickly.
Anyway, here’s a list of files in my current working directory.
Here, I have a number of files, so let’s use the file command against one of them.
file lltv_logo.png
As you can see, the file command told us what kind of file it is. It shows that it’s a PNG image, which is correct, that’s exactly what it is. But given the file extension, we already knew that. And most of the time, a file extension within the file name reveals what type of file something is, so no big surprise there.
But when it comes to Linux, adding a file extension to a filename isn’t required – it’s often added as a convenience.
Take this file for example:
sysadmin_cheatsheet
We have this sysadmin cheatsheet file, with no extension at all. And this is where the file command can really come in handy. From looking at the name alone, we have no idea what it is. So, let’s use the file command against it:
file sysadmin_cheatsheet
As you can see from the output, it’s a PDF file. And the cool thing about the file command is that it’s able to tell what kind of file something is, with or without a filename. So, whenever you run into a file with no extension, you can use the file command to determine what it is. That’s a very common use-case.
Another use-case for the file command is when you have a file that has an incorrect extension. Take this file for example:
linux_crash_course.jpg
This file is the logo I use for thumbnails within the Linux Crash Course series, and from the name – it appears to be a JPG file. However:
file linux_crash_course.jpg
…It’s not. The file is actually a PNG image, that was incorrectly named with a JPG extension. With the file command, we can verify that the extension given to a file matches the actual filetype. And on a server, this isn’t always going to be a common use-case. But even though file extensions are a convenience in servers, desktop versions of Linux use the file extension to determine what program to open a file with. If you have an incorrect extension, you might get an error message when you go to open a file. In that case, you might run the file command against a file to see if it was given the wrong file extension, which can happen.
Another cool trick with the file command is to not give it a filename at all, and instead have it reveal the filetype of every file in a working directory:
file *
As you can see, I get a nice list that tells me the file type for every file in this directory.
In addition, you can also use the file command to describe symbolic links as well:
file my-link
But what’s interesting here is that the file command not only tells me that the file is a symbolic link, it also tells me what it points to. In this case, this symlink points to a random file I chose, which is /etc/os-release. If you didn’t already know, that file contains information pertaining to the distribution version you’re running.
cat /etc/os-release
Back to the file command, you can see that it’s very intelligent. It gives you information about what type a file happens to be, and in the case of symlinks goes as far as to describe what the destination file happens to be. And sure, we already knew that from the ls command, but depending on how your shell is configured, you may not see the target shown when you run ls. With the file command though, it doesn’t matter – it will describe the file type and demistify what it is.
Often, a file you check will turn out to be a standard text file, like this one:
file bashrc
In this case, I created a backup copy of my bashrc file and saved it here, and it’s just a text file – nothing too exciting, but you’ll run into text files often.
Going back to symlinks, I wanted to show you that the file command is able to follow symlinks as well. Normally, the file command will report that a file is a symbolic link if it is:
file my-link
But if we add the -L option, the file command will tell us the filetype of the file that the symlink is linked to, instead of just reporting the link itself:
file -L my-link
And you know what? That’s about it when it comes to the file command. There are some additional options you can use if you check the man page, but most of those are useful only in very specific situations. The main take away is that the file command reveals the file type of a file, and that’s what you’ll use most of the time.


