How to Create a Magic File

How to Create a Magic File

Using the file command to create a magic file that shows all files type in a directory

You need to at least have a basic understanding of the shell as well as the terminal and basic shell commands to be able to articulate this. You can check my articles on the shell, navigation where I wrote about the aforementioned.

Introduction

Among all the shell commands, one of the most basic commands is the file command.

This command is used to identify a wide variety of file types e.g. text files, executable files, image files, audio files etc.

This command is quite useful as it can come in handy when one is troubleshooting files as well as for security purposes.

Troubleshooting comes into play when one is having a problem opening a file, you can simply use the command to identify the file type which can help you know the right tool to be used to open the file.

For security purposes, you can decide to identify the file type of a file that you are concerned about its content before opening the file to know whether it is safe to open the file or not.

What is a Magic File?

A magic file is simply a text file that contains a database of known file types. It is used by the file command to identify the type of file.

How The File Commands Reads the Magic File

The file command reads the magic file that is generated and compares the strings/numbers in the file whose type is to be checked to the strings/numbers in the magic file. If there is a match, the file command will identify the file as the type of file that is defined in the magic file.

How to Generate and Read a Magic File Using the File Command

Let us look at this simple task that will help us go through the whole process of generating and reading a magic file.

Create a magic file grit.mgc that can be used with the command file to detect Grit data files. Grit data files always contain the string GRIT at offset 0.

What this means is that we should create a magic file with the name grit.mgc, that can be used with the file command, this grit.mgc file should check for the string GRIT at offset 0, that is it is to check for the files whose contents begin with the word GRIT at the very beginning, and if found, it should return the name of the file data type as Grit data and Grit as the custom mime type of the file.

MIME stands for "Multipurpose Internet Mail Extensions". It is a standard way of describing the content of a file, such as text, images, audio, and video. MIME types are used by web servers, email clients, and other software to determine how to handle a file.

We can perform this task in several steps shown below:

Step 1: Create a Text File

Create a plain text file called grit, you can use the command:

touch grit

We named the text file grit because we are asked to create a magic file with the name grit.mgc, so the name of our file has to match the name of the magic file as the magic file will be formed from our file grit.

Step 2: Put the Magic Content into the File

Open the text file you just created (grit). You can use any text editor (vim, vi, emacs, nano etc.). Afterward, enter the following into the grit text file:

1. 0    string GRIT Grit data
2. !:mime Grit

On the first line of your editor, you put your 0, which is the offset, then press your tab button, followed by a string, since we want to check for a word, then followed by the word we want to check, which in this case is GRIT, then followed by the name of the file data type we want any file starting with the word GRIT to appear, which here is Grit data.

On the second line, you will enter the mime type of the file, which according to our task is Grit.

Next, you save and exit the file.

Step 3: Use File Command to Create the Magic File

Use the file command to create the magic file. You can do that using the command below:

file -C -m grit

We use the command file, followed by the option -C, with C in uppercase, tells the file command that we want to create a file, followed by the -m, which tells it that it is a magic file, so the command typically creates a magic file with the name grit.

After running this command, you should see the magic file that is created in the same directory as the grit file.

You can see how the magic file created is now grit.mgc.

Step 4: Check the File Data Type

To check for the file data type of all the files in the directory containing the magic file, simply use the command:

file -m grit.mgc *

After running this command, on my machine, I created 5 files to test for the magic content, so I used the exact string GRIT to start the content of the file 2, that is the real00 and real01, while I used the string Grit to start the content of the file 3 which is test, and lastly, I used the string GRIT for files 1, that is the fake00 and fake01, but not at the start, I put them in between.

You can see that base on the law we set for the magic content, file 2, real00 and real01 returns a Grit data, while the rest still carries GRIT, but not at the beginning of the text file thus, returns a file data type of ASCII text.

Below are the contents of the files:

The real00 file

The real01 file

The test file

The fake00 file

The fake01 file

Step 5: Check the MIME Data type

This is the final step, and this step is used to check the mime data type of every file in the directory, the command used here is:

file --mime-type -m grit.mgc *

You can see from the image above the MIME data type of the magic file grit.mgc as we set it in the grit file, which if you remember is Grit.

Conclusion

This is simply how to work with magic files using the file command on the shell.

I hope you enjoyed the tutorial and understand how to work with magic files now. I will love to see your comment on this. And please you can follow me on Twitter and LinkedIn and join me in my Software Engineering Journey. Thank you!