There are many Linux commands useful for beginners and trained users alike. One such command is the Linux file command. It is a typical application in Linux that determines what kind of data is saved in a specific system file. It reports the file type in a simple to understand format, otherwise known as a MIME type. In this tutorial, you’ll learn the basics of using the Linux file command, and how it can empower your VPS management.
Filenames in UNIX can be entirely independent of the file type, the Linux file command will easily reveal what kind it is! Whether it’s an audio, open document spreadsheet or text editor file, the Linux file command will give you its type in the readable form.
How to Use the Linux File Command
The command offers its users a wide range of uses. It helps understand the reasons why they can’t read a certain file on the system. File also helps determine the type of folder, socket, FIFO (pipes) and helps block special files. We can only view zero-length files in the Linux system by File command.
Linux File Command Syntax
Remember, before using the command you need to access your VPS with SSH. Check out our PuTTY tutorial if you’re having trouble. The basic syntax for the file command is as shown below:
file [options] [filename]
- file – instructs the terminal to execute the Linux file command
- [options] – is where you can add variables to the command
- [filename] – is the file you want to inspect by using the command
After executing the command, you will get an automatic reply from the system describing the type in a standard output format. It will also provide other beneficial information, like data stored in the file, the size of the file or the version of the file you have entered.
We created a simple text.txt file. Let’s use the most basic form of the Linux file command:
In the output we’ll be able to see that the file is an ASCII text file.
What to use the Linux File Command for
The File command analyses each argument to classify it. There are three sets of tests, conducted in this sequence: file system tests, magic tests, and language tests. The initial analysis that succeeds provides the file type.
Users conduct the file system tests to examine the return from a stat system call. The program reviews if the file is clear, or if it’s some kind of special file type. It looks for any known file types relevant to the system you are working on if the system specifies them in the system header file.
The magic tests are applied to control files with data fixed arrangements. The file command usually uses a database to manage the probing of lead bytes. That database is implemented in a file called magic, whose location is usually in devisers/file/magic or a similar one. These files possess a magic number saved in a particular place near the start of the file that shows Linux and other operating systems that the file is a binary executable.
Once file has determined the character set, it will then try to conclude in what language the file is written. The language tests look for special sequences that can appear anywhere in the first few segments of a file. These tests are less positive than the previous two groups, so these are performed later.
Using the Linux File Command with Options
The file command in Linux is valuable as filenames in UNIX have no connection to their file type. So a file called devisers.csv could be a zip file. The below command can test this:
If you only want to view the file type then use the –b option in the terminal along with the filename:
file –b devisers.txt
The file command can also work on multiple files available in the system and reflect the output in separate lines for each file. To do this, simply replace a variable with a wildcard – *:
The output will display the information on all .txt files in the current directory.
The –i option is used to view the mime type of the file which is not in a readable format. The command would look like this in the command line:
file –i devisers.csv
To view special files, use –s which allow the user to read the argument files which are not working or have special characters. It also provides information regarding the size of the special files. The command to view the special file is as below:
file –s txt.t
Remember to add the name of the actual file you would like to inspect!
It’s clear that the Linux file command is a useful utility for numerous ways of generating long algorithms according to your needs. All you need is to specify the file and use the command in the correct syntax.
The file command is great for understanding precisely what you have on your machine.
Once you get the hang of this command, you will learn more about different file types present in your system, successfully improving your project. Happy developing!