Jan 30, 2023
Edward S. & Noviantika G.
The Linux File Command: What Does It Do and How to Use It to Determine a File Type
In UNIX-like systems, file names can be entirely different from their actual types. In some cases, they don’t even have valid extensions. Therefore, it can make managing data more complicated.
To organize information easily, Linux provides a program called the file command. It’s primarily used to determine the file type – either American Standard Code for Information Interchange (ASCII) text or Multipurpose Internet Mail Extensions (MIME) format.
In this tutorial, you’ll learn the basics of using the program and how it can empower your server management and Linux operation skills.
Understanding the Linux File Command Syntax
To use the Linux file command on a VPS hosting, you’ll have to connect it with an SSH client such as PuTTY or Terminal.
Before we get to it, let’s analyze the basic syntax of the file command:
file [options] [file name]
- file – instructs the shell to execute the file command.
- [options] – modifies the command’s operation.
- [file name] – inserts the file name you want to inspect.
When executed, the command doesn’t consider the file extension. Instead, it runs three tests to determine the file type:
- Filesystem test – examines the return from the stat system call. The program reviews if it is an empty file or a special file type. It also looks for known formats relevant to the system you work on if they’re specified in the system header file.
- Magic test – uses magic numbers, a short string of numbers at the beginning of a file, to check whether it is binary executable data. If it is, the program will determine its type. The necessary information to run this test is available in /etc/magic or /usr/share/misc/magic from a compiled magic file.
- Language test – examines the character sets the file is written in, such as ASCII text or UTF-8. It also looks for any special sequence that appears in the first few lines. However, this test is less accurate than the previous two, so it’s performed last.
The command’s output displays the file type using the standard format. Depending on the command option, it may provide other information, such as data stored in compressed files, size, or version.
Before practicing the command options, use the nano editor to create a sample text named test.txt:
Once the command line opens a new file, write a few lines of text and press Ctrl + X and Y to exit and save the changes.
Enter the Linux file command to check whether the test.txt file was created successfully:
The output will show that an ASCII text file was created:
Ways to Use the Linux File Command
The option in the syntax allows you to add variables to the Linux file command. Here are some of the most common ones:
- -b or –brief – fetches a short description of the file type.
- file * – lists all file types in the current working directory.
- -i or –mime – shows the MIME file type.
- -s or –special-files – reads special files.
- -z or –uncompress – checks and displays information inside compressed files.
- -c or –checking-printout – checks a magic file’s parsed version.
- -m or –magic-file – utilizes an alternative magic file provided by the user.
- -d – displays internal debugging information using the standard format.
- <regex range> – fetches file types in specific ranges.
- -0 or –print0 – prints a null character at the end of the file name.
- –help – shows the file command’s help message. It also lists acceptable options and their usage.
In the following sections, we’ll discuss how to use each of these options.
Check the File Type
In Linux, while users can rename their files, the updated information may not represent the actual data. To find the correct type of a file, enter:
For example, you rename test.txt to text.zip. To reveal the valid file type, enter:
The output will display the name and its actual type, an ASCII text file:
To view the format in brief mode, use the -b option on Terminal, followed by the file name. For example:
file -b text.zip
The output will show the file type without its name:
List the File Type of Multiple Files
The file command can list each file type in the home directory. To do this, enter file and add a wildcard character (*):
The program will show all the files and directories:
In addition, the file command can show each file type inside a specific directory. Here’s the general syntax:
Find the MIME File Type
The -i option is used to view the MIME file type. It consists of two parts – a type and a subtype. MIME uses a slash (/) to separate each of them, with no space in between.
Here’s the general syntax:
file -i filename
For example, to view the MIME type of the test2.txt file, enter:
file -i test2.txt
Here’s the output of the file command above:
Instead of declaring the file format as ASCII text, the program defines the file as text/plain and charset=us-ascii.
Read the Special File Type
The file command allows you to read special files, such as system information, by adding the -s option.
Important! Keep in mind that only a root user can run the file command along the -s option. Otherwise, you’ll receive a no-read permission error message.
This option only classifies a file as a block special file, symbolic link, directory, or nonexistent.
Here’s its general format:
sudo file -s filename
For example, to read the ploop19269 file, enter:
sudo file -s /dev/ploop19269
The output indicates that ploop19269 is a DOS/MBR boot sector.
Read a Compressed File
There are two ways to check compressed files like ZIP or gzip archives, the -z and -Z options. The former displays detailed information and its content, while the latter only shows the file types.
Here’s the general syntax of the -z option:
file -z filename
For example, to read the test2.txt.gz file’s complete data, enter:
file -z test2.txt.gz
The output specifies that test2.txt.gz is a gzip compressed file that contains test2.txt:
Here’s the general format of the -Z option:
file -Z filename
For example, to view the file type of test.gz only, enter:
file -Z test.gz
This command will only print out the type of the file inside test.gz – an ASCII text.
Test Parsed Version of a File
Adding the -c option allows you to view the parsed version of any file. It shows information such as type, opcode, and value. Usually, it is used in conjugation with the -m option to debug a new magic file before installing it.
Here’s its general syntax:
file -c filename
For example, to print the parsed form of the test.txt file, enter:
file -c test.txt
The output should look like this:
List File Types
The file command lists all file types in a directory using the Regex-style ranges. Type file and place the values in brackets, followed by *.
Its general syntax is:
For example, to examine files starting within the a to z range, enter:
The output should look like this:
Since this program is case-sensitive, the output will only show the files starting with a lowercase a to z. To include the uppercase characters, add another range. For example:
file [a-z]* [A-Z]*
Here’s what the output looks like:
In UNIX systems, file names and extensions can differ from their actual types. Hence, Linux provides the file command to help users determine the type of a file.
When executing it, use appropriate options and specify the file name. There are many acceptable variables to use with the file command, such as:
- -c – tests the parsed form of a file.
- -i – finds the mime type.
- * – lists multiple files.
- -z – reads compressed content.
We hope this article has helped you learn how to manage data using the Linux file command. If you have any questions or suggestions, please leave them in the comments section below.
Learn More Linux Commands for File Management
Remove Directory in Linux: How to Delete Files and Folders
How to Use the Linux Locate Command to Find Any File
How to Use the Tar Command in Linux
How to Rename a File
How to Create Linux Symlinks (Symbolic Links) for Files and Directories
Linux File Command FAQ
In this section, we will answer the most common questions about the Linux file command.
What Exactly Does the Linux File Command Do?
File names in UNIX can be entirely independent of the file types. Thus, it’s tricky to determine the actual information.
Executing the file command reveals what format a file uses and examines each argument by conducting three tests – filesystem, magic, and language tests. The first that succeeds will output the file type.
Which Linux Command Creates a Blank File in the Current Directory?
To create one or multiple empty files, use the touch command. It comes with the Linux system and is especially useful when you don’t have data to store at the time.
Its general syntax is: touch filename. To create multiple files, enter: touch filename1 filename2.