Skip to main content

File Properties Commands

file Command Examples

Here are various examples of the file command in Linux to determine the actual type of file and gather related information.

You have seen commands to create file in Linux. You have also seen the command to delete file in Linux. Let’s talk about the file command in this article.

What is the file command in Linux and Unix?

How do you recognize the type of a file? Let me guess, by its extension, right? I mean if you see a file named MyData.csv, you guess that the file is in CSV format. You can make sure of that by viewing the file content.

But the file extensions in Linux and Unix have no real meaning. You can name a csv file, a zip file. You can choose to not use any extension at all.

The file command comes handy in such situations. The file command in Linux determines the actual type of a file, no matter what its extension is.

It has a simple syntax with only a few options:

file [option] filename

Now that you know the syntax let’s see how to use the file command.

Example of file command in Linux

In its simplest form, you can use the file command with filename or path to file and it will show the type of the file.

file filename

Here’s an example:

abhishek@linuxhandbook:~/$ file cpluplus.cpp
cpluplus.cpp: C++ source, ASCII text

Let’s see some other ways you can use it with its options.

Remove filename from the output

You can use the option -b and the output will show only the file type omitting the filename. It could be useful in scripting.

file -b filename

Have a look at the same example you saw earlier:

abhishek@linuxhandbook:~/$ file -b cpluplus.cpp
C++ source, ASCII text

Get the mime type of the file

You can also display the MIME type of the file thanks to the -i option.

file -i filename

Here’s an example of the command on a video file, with and without MIME type info:

abhishek@linuxhandbook:~/$ file my_video.mp4
my_video.mp4: ISO Media, MP4 v2 [ISO 14496-14]
abhishek@linuxhandbook:~/$ file -i my_video.mp4
my_video.mp4: video/mp4; charset=binary

You can combine -b and -i options.

Get file type info of a file inside an archive file

If you gzip a directory and now you have a compressed file. You can examine the uncompressed contents to decide the file type with -z option.

file -z compressed_file

Let me show you an example with and without the -z option:

abhishek@linuxhandbook:~/$ file author-pro.zip
author-pro.zip: Zip archive data, at least v2.0 to extract
abhishek@linuxhandbook:~/$ file -z author-pro.zip
author-pro.zip: PHP script, ASCII text (Zip archive data, at least v2.0 to extract)

Use file command with multiple files

File command can be run on multiple files simultaneously.

file file1 file2 file3

Here’s an example for you so that you can see it in action:

abhishek@linuxhandbook:~/$ file cpluplus.cpp agatha.txt bash_script.sh
cpluplus.cpp: C++ source, ASCII textagatha.txt: ASCII text
bash_script.sh: Bourne-Again shell script, ASCII text executable

Use file command with regex

If you want to use the file command on multiple files, you don’t always have to provide all the filenames. You can use regex instead.

It’s really up to your requirement and imagination how you could use it. I’ll show some examples nevertheless.

If you want to display file type of all the files in the current directory, simply use this:

file *

If you want to display file type of all the files with a certain extension, you can do that as well:

file *.txt

The possibilities are endless. Want to display file type of files with name starting with ‘a’? Use this:

file [a]*

Other options you may use with file command

Here are a few other options with file command that you may use:

  • -L : Follow symbolic links and report the type of the destination file
  • -f file_name : Read filenames line by line from the given file_name and report their file type

There are a few other options as well but I believe you have learned all the essential examples of the Linux file command. If you have questions or suggestions, do let me know in the comment section.

Abhishek Prakash