Managing data and files is one of the tasks of Linux system administrators for their better performance in the direction of their goals. Managing data and files in Linux systems is not as simple as you think. Linux includes various standard files such as Symbolic link, Regular, Directory, Block special, FIFO special, and Character special, which are usually categorized into 3 categories: Regular Files, Directory Files, and Special Files.
The easiest way to detect the type of file in Linux is to check the extension of the desired file, which is usually ‘.txt’, ‘.py’, or ‘.go’; but since files usually do not have a valid extension or sometimes their actual types are in contradiction with the file name, determining the type of file and managing them becomes a bit complicated.
Linux users need a useful solution to manage and organize information, run files using programs, and access more information about files. Using the file command is an efficient solution to determine the file type in the Linux system.In Linux, there are many commands for different purposes, we have previously taught the most useful Linux commands comprehensively, the Linux file command is one of the most useful Linux commands in data management.
In this article, we will teach how to use the file command to improve the management performance of Linux VPS or any Linux system, and by using the file command and the instructions provided in this article, you can easily recognize the file type in Linux.
To execute the following instructions, you must use a Linux VPS or a system that is running one of the Linux distributions. Also to execute the file command in the Linux VPS, it is better to connect to the VPS through an SSH client such as PuTTY and Terminal.
Introduction to Linux File Command Syntax
The main syntax of the file command is in the following format:
file [options] [file name]
- file: the file character is typed to execute the command.
- option: typed to change the operation of the command, you can replace different variables for different purposes.
- file name: In this section, replace the name of the file that you intend to test and analyze its content.
By executing the main syntax of the file command, tests are performed to detect the type of file, which we will explain below:
Filesystem test: This test is performed to check the stat(2) system call and checks the result with the system header file to find out if a file is empty or a special type of file. In addition, if it is specified in the system header file, it looks for a familiar format related to the system you are working on.
Magic test: By using the string of numbers that are stored at the beginning of a file, which is known as magic numbers, to determine the type of file, it checks whether executable files are binary. To access information related to such tests, you can use /etc/magic or /usr/share/misc/magic from the compiled magic file.
Language test: This test is performed to check the set of characters included in the file, such as ASCII text or UTF-8, and by using a particular tag, it determines the programming language of the binary executable file. Also, this test provides information in the output about the type of file and the type of data stored in the file, their size, and version according to entering the different options in the file command. Language test analyzes them by observing the particular sequence in the first lines, but in general, two tests, the File system test and the Magic test, are more accurate than this test.
For example, if you want to test the text file by running the file command, you must enter the command as follows:
example.txt: ASCII text
So in the output, you will see that the ASCII text file is created.
Functional options in Linux file command
You can use the Linux file command for different purposes by using the options that we explain below. It is enough to add the options that we will introduce to you as different variables to the file command so that you can achieve more useful results. In the list below, we have compiled the most common options for using the File command:
–apple: The function of this option is to change the output of the command to the output used in non-updated versions of macOS.
-b, –brief: Run to obtain a brief description of the file type.
-C, –compile: It is used to create the output file that contains the pre-parsed version of the magic file.
-c, –checking-printout: checks the parsed version of the magic file.
-d: Runs to get internal debugging information in the standard format.
-E: This option is used to send an error message about a filesystem error.
-e, –exclude: by running this option among the list of tests that have been run on a file, a specific test is excluded.
–exclude-quiet: It is used to exclude the test that the file command does not support.
–extension: It is used to find valid extensions in determining the file type.
-F, –separator: separates the file name and file type from each other through the provided string.
-f, –files-from: List files to test using the provided text file.
file *: Runs to get a list of all types of files in the current working directory.
-h, –no-deference: If you want to stop following symbolic links, this option is implemented to disable this feature.
-i, –mime: It is executed to obtain the type of MIME files.
–mime-type, –mime-encoding: It is used to change the output to a MIME type string (It provides encryption or type).
-m or –magic file: This option enables the use of an alternative magic file provided by the user.
-k, –keep-going: It is used to continue testing after providing Match results.
-l, –list: used to provide a list of matching patterns in descending order.
-L, –deference: used to activate the ability to follow symbolic links.
-P, –parameter: It is used to set parameters such as count, max bytes or recursion, etc.
-r, –raw: By adding this option, the translation of non-printable characters is prevented.
-S, –no-sandbox: It disables the sandbox in compatible systems with it.
-s, –special files: special files can be read by adding this option.
-v, –version: used to provide the command version of the file.
-z, –uncompress: Analyzes compressed files.
-0, –print0: Displays a null character at the end of the file name.
<regex range>: provides file types in the specified range.
–help: provides a help message in the file command and a list of acceptable options.
You are familiar with the options of the file command, in the following, we will discuss how to use these options in the file command.
How to use the file command
Now that you are familiar with the functional options in the file command, we will come to the useful part of the article, where you will better understand how to use these options to manage data and determine the type of files with our examples. So stay with us until the end of the article.
Finding the file type
One of the problems in checking the file type in Linux is that users may not get real data by renaming their files after updating the information. To solve this challenge and correctly identify the type of files, you can use the following command:
For example, if you renamed your file from example.txt to text.zip. To access the valid file type, you must run the following command:
text.zip: ASCII text
As you can see, the output shows the actual name and type of the file (in our example it is an ASCII text file).
If you want the output of the file command, which determines the actual type of the file, to be presented in a concise mode, you must run the following command in the terminal:
file -b text.zip
The -b option will summarize the output.
testing multiple files
To check multiple files at once, follow the pattern below as a guide:
file [file name 1] [file name 2] … [file name n]
For example, if you want to test a text file, a webpage, and a photo at the same time, the command you must execute will be as follows:
file example.txt index.html sample.png
Checking the file type of several files through the listing
If you want to list all types of files and directories in your working directory using the file command, you must run the following command:
Package.json: JSON data test.txt: ASCII text text.zip: ASCII text
The file command even allows identifying the type of file in a specific directory. To do this, use the following syntax:
listing all types of files in a specific range
Sometimes you need to check the types of files in a specific range, for this, you have to list all types of files using the file command and Regex-style range. Therefore, the general syntax for this purpose is as follows:
For example, to display the types of files in the range A to Z, you must execute the command as follows:
file [A-Z] *
Note: capitalization of letters in this command is important, so the output of the above command includes the files whose names start with uppercase characters in the specified range. If you want to check files with smaller characters in the specified range in addition to the files whose names start with uppercase characters, you should add lowercase letters to the above command as follows:
file [A-Z]* [a-z]*
Testing files using a list of files
In this method, a text file is used as a list of files as follows:
file -f filename
file -f list.txt
As you can see, to achieve our goal in this section, you will add the -f option along with the path to the list file to the file command.
Note: the content of the text file you use must be such that there is a file name in each line.
Adding a Separator to output
In the file command, by adding the -F option, you can use the specified character to separate the file name and file type sections in the output. Consider the following example to better understand how you can separate the output using the file command. In our example, the (+) character is specified as the separator:
file -F + Example example.txt sample.png index.html
MIME file type detection
As we provided a list of options at the beginning of the article, to detect the MIME file type, you must add the -i option to the Linux file command. This command contains two sections, type, and subtype, which MIME separates from each other using / without spaces. To identify the MIME file type, the main syntax is as follows:
file -i filename
For example, if you want to access the MIME type of the example1.txt file, enter the following command:
file -i example1.txt
example1.txt: text/plain; charset=us-ascii
Therefore, the output of this command will contain the file text/plain and charset=us-ascii instead of providing ASCII text file format.
Reading special file type
By adding the -s option to the Linux file command, you will be able to read the specific file, and if the file command does not provide complete information about the specific file, you can read special files by adding the -s option. Of course, the execution of this command requires the permission of the Root user, otherwise, you will encounter the error message permission to read. The main syntax for reading a special file is as follows:
sudo file -s filename
This command categorizes the file as a directory, symbolic link, or block special file.
Compressed file reading
In order to be able to read compressed files (for example, ZIP or gzip files) through the file command, you can use two methods:
- Using the -z option: provides more details and information.
- Using the -Z option: It provides all kinds of files.
The general format of the file command with the addition of the -z option is as follows:
file -z filename
For example, to read the full content of the example1.txt.gz file, you must execute the following command:
file -z example1.txt.gz
Example1.txt.gz: ASCII text (gzip compressed data, was “example1.txt” , last modified: sun sep 8 06:10:35 2022, from Unix)
The general format of the file command using the -Z option is as follows:
file -Z filename
For example, to determine the type of example.gz file, you must enter the following command:
file -Z example.gz
Example1.txt.gz: ASCII text
Removing File Name Padding from the output of the file command
If you want to remove the padding between the type and file name sections in the output of the file command, enter the -N option to the file command according to the following example:
file -N Example example.txt sample.png index.html
Testing the Parsed version of a file
To view the parsed version of a file by running the file command, you must add the -c option to the command to get information about the opcode, type, mask, offset, and value.
The basic syntax for testing the parsed version of a file is as follows:
file -c filename
Sometimes to debug a new magic file, the above command is also used along with -m.
In Linux or Unix systems, files are divided into 3 categories, Regular Files, Directory Files, and Special Files.
- Text file (TXT)
- Microsoft excel files (XLS and XLSX)
- Word documents (DOC and DOCX)
- PDF file
- Hypertext markup language (HTML and HTM)
The file command in Linux is used to determine the file type, which examines and tests the content of the file and then determines the file type.
To manage files and data in Linux systems, you will need to access all types of files and check and test the files, which use the file command is very efficient in determining the type of files and data management for Linux users. In this article, we have taught the most common methods of using the file command. We hope this training will be useful for you in data management.
Thank you for choosing our article to read.