The Linux file command is a commonly used command to determine a data type of a specific file. In this article, we will be learning the basics of the Linux file command to have better management of our Linux based VPS. Before using the command, access your VPS using SSH.
File Command Syntax
file <option> <file_name>
The command above is the basic syntax of the file command, and here is what it does.
- file – The file command, to be executed in the terminal.
- <option> – To add variables into the file command.
- <file_name> -The name of the file that will be inspected by the file command.
You will receive a reply from the system regarding the type, size, and more information about the file inspected when executing the command. For example, create a text file named “text.txt” and inspect it with the file command.
file text.txt
An output display will reveal that the file is an ASCII text file.
File Command with Options
The file command is extremely valuable in Linux as filenames do not have any connection regarding the file type. For example, a file named “test.csv” might actually be a zip file instead. To reveal the type of file it is, run the following command.
file test.csv
However, this command doesn’t only print the file type but also other information regarding it. To display only the file type, you may add the “-b” option in the command as shown in the following command.
file -b test.csv
Aside from that, you may also use the file command on multiple files at once. For example, within a directory with multiple text files in it, you may inspect all text files within the directory using a wildcard, “*”, the asterisk symbol. The following command is an example of how to inspect all text files within the same directory.
file *.txt
This command will display all the information regarding all the text files inspected within the same directory.
To read the mime type of the file, which isn’t in the readable format, use the “-i” option to assist you. The following command would be an example of the how the command would look like.
file -i test.csv
Finally, use the “-s” option to view special files such as argument files that consist of special characters or not working. The following command shows an example of using the “-s” option.
file -s txt.t
The file command is an extremely useful utility regardless for a newbie or trained user. Hopefully learning the basics of file command will assist you in managing your VPS.