Simple File Command:
Let’s have a few illustrations of using Linux commands with different options. We will start with creating a simple text file with “touch” instructions in the Linux shell. So, we will be using the very basic file command in the shell to check the current file type, as shown in the attached screenshot image below. As the file is empty, it will show that the file is now blank.
Now, we have added some content in the file and displayed the content of this file on the shell. For this purpose, we have utilized the “cat” command. When we used the “file” command again with the file name, we have got the file type as “ASCII text”. If the file is of another time, it will display that particular type on your screen.
This simple command can also be used for the contents of an identified location. So, we have tried it with the “etc/group” location. It shows that this folder contains an “ASCII” type file.
The same file command has been applied to another location. It shows this folder contains only the “directory” type and not a single file in it.
File Command with Steric (*) Character:
If a user wants to check the type of a file or folder in a current location of Linux, he/she can use the file command with the “*” character. This instruction will demonstrate all the files and folders of a particular location, i.e., directory with their type. Now, we are currently held at the home directory of our Linux system. We have used the file command with the steric sign shown below. Upon the execution of this command, it displays all the files and folders of the home directory and their type. Some files are “Html documents” having ASCII text type. Some are simple text ASCII files. Some folders are simple directories but not files.
Let’s have a look at how the “file” command with the “*” sign works on the particular folder. For that, we need to state the location of a folder. So, we have tried it on our system’s “Documents” folder. You can have an idea of the Documents folder with the shown output. It shows that the folder has only three files in it. One of the files is a “C” type file having some ASCII text. One of them is the “Html” type document with ASCII content, and the third is a simple text file, i.e., “ASCII text”.
File Command with “-b” Flag:
If you want to only see the type of file, you can use the “-b” flag within the “file” command in the shell. The command has been executed down in the image and shows the file type.
This “-b” flag in the “file” command can also be used for the particular folder, i.e., Documents. You can have a look that it displays only the types of files and not the file names as it does in the “*” character file command above. It simply shows that there are only three files in the Documents folder having the specified type.
File Command with “-i” Flag:
Another thing we can get from the file command is its MIME type. The MIME type means to know about the file’s extension and the type of data or content it can store. This means we will be shown with the file extension on our output screen. So, let’s use the file command with the “-I” flag with the particular file name, i.e., “new.txt” in the shell. This file is currently located in the home folder. In return, it displayed that the file “new.txt” contains a “text/plain” extension on its end and should have some simple plain text data in it. It also shows the character set of this particular file which is the “us-ASCII” type.
Now, we have used this “-i” flag in the “file” command for the particular folder, i.e., Documents with “*” character. In return, it displays all the files currently held in the Documents folder with their extension type name and the content type, i.e., C-extension, html, or plain text
File Command with “-s” Flag:
The “-s” flag shows the MIME type of a file more briefly and clearly. You can see the result it displays as extensions of files, i.e., C source, Html document, and ASCII text.
File Command with Range:
You can also define the range of a file to be checked for a type and display on the shell from a specified or current folder. The range in alphabets, i.e., a to z, will be defined within the square brackets. So, within our first illustration, we have tried to get the type of a file having names starting from alphabets “a” to “d”. It returns a single result, as revealed below.
Specifying the range between “a” to “z” returns 3 of the total results.
Finally, we have done a simple illustration of using the “file” command in the Ubuntu 20.04 Linux system. We have utilized many flag options in our illustration to see different perspectives and usages of the “file” command of Linux. It shows that the “file” command is quite versatile and multipurpose. With this, we extremely believe that this article will prove itself a great help.