Skip to content

file command in Linux

The Linux file command is used to determine the file type and encoding of a specified file. It can identify whether a file is a text file, binary file, or a compressed file. By running the file command followed by the name of the file, users can quickly obtain information about the file type. This can be helpful in various scenarios, such as when working with unknown files or needing to verify the format of a file. The file command is a versatile tool for understanding the characteristics of files on a Linux system.

Terminal window
file [option] [parameter]
OptionDescription
-bDo not print anything except the type
-dShow just the device number
-iShow the inode number
-kShow the block counts
-lShow a symbolic link
-LFollow symbolic links
-nShow the file’s numeric ID
-sShow the size in bytes
-tShow the file type
-zShow the last timezone change
ParameterDescription
fileThe file(s) to determine the type of
Terminal window
file example.txt

Determine the type of file “example.txt”.

Terminal window
file file1 file2 file3

Check the file type of multiple files: “file1”, “file2”, and “file3”.

Terminal window
file -s example.jpg

Display summarized file information for “example.jpg”.

Terminal window
file /path/to/directory/*

Check the file type of all files in a specific directory.

Terminal window
file -r /path/to/directory

Recursively display the file type of all files in a directory.

Terminal window
file -i example.pdf

Show only the MIME type of the file “example.pdf”.

Terminal window
file --mime-type example.docx

Check the file type of “example.docx” based on its extension.

Terminal window
file -i https://example.com/file.zip

Determine the MIME type of a remote file “https://example.com/file.zip”.

To use the file command in Linux, execute the following command:

Terminal window
file --option <value>

What is the purpose of the file command in bash?

Section titled “What is the purpose of the file command in bash?”

The file command in bash is used to determine the type of a file.

How can I get detailed information about a file using file in Linux?

Section titled “How can I get detailed information about a file using file in Linux?”

To get detailed information about a file in Linux, you can use the following command:

Terminal window
file -i filename

How to check the file type using file in bash?

Section titled “How to check the file type using file in bash?”

You can check the file type using the file command with the -b option. Here’s an example:

Terminal window
file -b filename

How can I determine the MIME type of a file in Linux using file?

Section titled “How can I determine the MIME type of a file in Linux using file?”

To determine the MIME type of a file in Linux, you can use the following command:

Terminal window
file -i filename

How to identify the encoding of a file in Linux using file command?

Section titled “How to identify the encoding of a file in Linux using file command?”

You can identify the encoding of a file in Linux using the following command:

Terminal window
file -bi filename

How can I check if a file is an ASCII text file using file in bash?

Section titled “How can I check if a file is an ASCII text file using file in bash?”

To check if a file is an ASCII text file in bash, you can use the following command:

Terminal window
file -i filename | grep "charset=us-ascii"

How to recursively check the file type of all files in a directory in Linux using file?

Section titled “How to recursively check the file type of all files in a directory in Linux using file?”

You can recursively check the file type of all files in a directory in Linux using the following command:

Terminal window
file /path/to/directory/*

How to display only the file type without additional information in Linux using file command?

Section titled “How to display only the file type without additional information in Linux using file command?”

To display only the file type without additional information in Linux, you can use the following command:

Terminal window
file -b filename
  • Checking the type of a file
  • Determining the encoding of a file
  • Identifying the file format
  • Inspecting file contents
  • Handling unknown file types