The file command in Linux

Hello and welcome to this post. Today, you will learn about the file command in Linux. This basic command allows you to perform some operations on files.

What does the file command do in Linux

file is a command of the Unix family of operating systems, which allows detecting the type and format of a file (Wikipedia). Being a basic command for file analysis, it is possible to have it in all Linux distributions.

Being a bit more technical, the file command performs a series of tests to determine the type and format of a file. This is its main utility.

You may wonder if this command is useful, and the truth is that if you work a lot in the terminal, you will notice that files do not always have their extension. So many sysadmins keep it in mind for various operations.

So let’s go for it. Let’s explain the use of the command by examples.

Using the file command in Linux

The file command requires a file as the main parameter. Therefore, we need a file.

To use the file command, you just have to call it together with a file.

file [file-path]

For example,

file example

If the file is empty, you will get a screen output similar to this one

 example: empty

In case the file already has text in it, the output will look like this

 example: ASCII text

If you want the file name not to be displayed in the output, then you have to add the -b option.

file -b example

Output:

ASCII text

If we are going to use the file command, it is because the file has no extension. In this case, you should use the --mime-type option.

For example,

file --mime-type /etc/apt/sources.list

Output:

/etc/apt/sources.list: text/plain

If you work a lot with text files, then the --mime-encoding option will be very useful because with it, you can find out the character encoding.

file --mime-encoding [file]

Now, usually you work a lot with compressed files, if they are not password protected, you can have --file scan inside.

file -z example.zip

The file command leaves traces on the access date of the file. If you want to prevent this, then use the -p option.

file -p example

Remember, as with many commands in Linux, you can mix several options to get more accurate results.

If you want to parse many files at once, you can add them all into one file and have file read them from there.

Example,

nano files.txt

Next, set some files and their paths.

/etc/fstab
/etc/hosts
/home/user/hello.txt

Now to scan these three files at once, you can run

file -f files.txt

Then the file command will read each of the files in this text file. Ideal if there are many files to scan.

Finally, we can parse many files at once by using wildcards. For example, to scan all files in a directory.

file *

Or only text files:

file *.txt

Or files starting with a range of letters. Example

file [a-d]*

Finally, if you want to know more about this command, I recommend you to take a look at the command’s help.

file --help

Conclusion

This command allows you to analyze various files to find out what their extension is. This makes it an important tool for many people who work a lot in the terminal.

Share This:

Leave a Comment

Your email address will not be published.