The “file” Command in Linux [9+ Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The file command in Linux determines the types of files on your system. It runs tests on the provided file/s to categorize them and then displays the final output. In this article, I will demonstrate the uses of the file command with practical examples.

A. Description

The file command in Linux tests each file passed as an argument to determine its classification. During the process, the system runs three types of tests and the succeeding test determines the file type. The descriptions of these tests are given below.

  • filesystem test: Checks whether the file is empty or if it is of a special type. This test examines the return from a stat system call.
  • magic test: Checks for files with fixed data formats. It uses the concept of “magic number” that indicates extensions of data files.
  • language test: Determines in what language the file is written. It looks for a particular string that can appear anywhere in the first few blocks of the file.

B. Syntax

The syntax of the file command in Linux simply contains single or multiple options and then the desired Filenames.

file [OPTION]... FILE …

Note: In the above syntax OPTION enclosed by square brackets and followed by 3 dots represents that multiple options can be utilized simultaneously. Similarly, 3 dots after FILE suggest that you can specify multiple files at once.

C. Options

The file command in Linux offers several options to modify your experience with files. You will find the most useful ones here for your convenience. For further features, you can look at the man page.

man file

Useful Options

  • -b: Does not print filenames with output.
  • -c: Prints out the parsed form of the file.
  • -d: Shows debugging information of a file.
  • -f: Reads file names from a namefile.
  • -i: Gives mime-type strings as output.
  • -s: Determines the type of special files.
  • -z: Determines the type of compressed files.

NB: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.

Practical Examples of the “file” Command in Linux

The file command in Linux is a useful command used to learn about the files available on your machine. In this section, I will illustrate some of the most helpful applications of the file command.

Example 1: Determining the Type of a File Using the “file” Command in Linux

You can determine the type of a certain file using the file command in Linux. In this example, I will find out the type of the file “jobs.txt”. To do so you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

file jobs.txt

➌ Now, press the ENTER button.

Output >

In the image below, you can see that I have displayed the type of the desired file.Determining Type of a File Using the file Command in Linux

Example 2: Determining Types of Multiple Files Using the “file” Command in Linux

You can find out the types of multiple files using the file command in Linux. You can provide a list of files separated by a space or pass regex ranges to indicate the number of files. Below are some examples of accessing multiple files using the file command in Linux.

Case A: Printing Types of All Files Available in the Current Directory

To display all the file types in the current directory use the wildcard character “*” and follow the steps below.

Steps to Follow >

➊ At first go to the Ubuntu Terminal.

➋ Type the following command in the command line:

file *

➌ Now, press ENTER.

Output >

In this given image, you can see that I have shown all the file types available in my current directory.Printing Types of All Files Available in the Current Directory.

Case B: Printing Types of All Files Available in a Specified Directory

To print all the file types of a specific directory follow the below process.

Steps to Follow >

➊ At the first launch the Ubuntu Terminal.

➋ Write the following command in the command prompt:

file Downloads/*

➌ Hit the ENTER button.

Output >

In the following image, you can see that I have displayed the types of all the files available in the “Downloads/” directory.Printing Types of All Files Available in a Specified Directory.

Case C: Printing Types of Files From a Given List

To print types of files from a given list follow the below steps.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

file jobs.txt file2.txt file1.txt

➌ Now, press the ENTER button.

Output >

In the given image, you can see that I have printed types of all 3 files provided within a list.Printing Types of Files From a Given List.

Case D:  Printing Types of Filenames in a Range

You can find types of a set of files by providing a regex style range with the help of the file command in Linux. You need to place a range inside square brackets and assign the wildcard character “*” with it. However, remember that the result for this command will be case-sensitive. In this example, I will print the files with names in the range [a-g]. To do the same follow the given procedure.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

file [a-g]*

➌ Now, press the ENTER button.

Output >

In this image, you can see that I have printed all the files having names starting with a-g.Printing Types of Filenames in a Range.


Similar Readings


Example 3: Examining Files Listed on a Text File Using the “file” Command in Linux

You can examine a list of files written on another text file using the file command in Linux. To do so you will need to use option -f. In this example, I will find out the types of files written on the namefilefiles.txt”. You can follow the steps below to do the same.

Steps to Follow >

➊ At first go to the Ubuntu Terminal.

➋ Type the following command in the command prompt:

file -f files.txt

➌ Press the ENTER button.

Output >

In the image below, at first, I showed you the namefile contents using the cat command. Then, you can see that I have displayed all the file types from the namefile.Printing File Types Listed on a Text File Using the file Command in Linux

Example 4: Testing Special Files Using the “file” Command in Linux

You can determine the type of special files (i.e. Device files etc.) using the file command in Linux with the option -s. You will need to run commands as a superuser using the keyword sudo to access the special files. In this example, I will find out the type of the special file “sda1”(The sda1 is a special file because it is a device file that represents the first partition of the hard disk.). To do so you can follow the steps below.

Steps to Follow >

➊ Launch the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo file -s /dev/sda1

➌ Hit ENTER.

Output >

In the image given, you can see that I have displayed the type of the desired special file.Printing types of Special Files Using the file Command in Linux

Example 5: Testing Compressed Files Using the “file” Command in Linux

You can find out the type of compressed files using the file command in Linux with the option -z. In this example, I will find out the type of the compressed file “compressedfile.zip”. To do the same you can follow the below steps.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

file -z compressedfile.zip

➌ Type your Password.

❹ Now, press the ENTER button.

Output >

In the following image, you can see that I have shown the type of the “compressedfile.zip” file.Showing types of compressed files using the file command in Linux.

Example 6: Testing Parsed Version of a File Using the “file” Command in Linux

You can test a parsed version of a file using the file command in Linux with the option -c.  The parsed version of a file contains the rules and patterns used by the system for the file’s magic test. In this example, I will display the check printout for the parsed version of the file “file2.txt”. To do so you can follow the given process.

Steps to Follow >

➊ At the first launch the Ubuntu Terminal.

➋ Type the following command in the command prompt:

file -c file2.txt

➌ Now, hit the ENTER button.

Output >

In the image below, you can see that I have printed the parsed version of the file “file2.txt”.Displaying parsed version of a file using the file command in Linux.

Example 7: Displaying File Types in Brief Mode Using the “file” Command in Linux

You can display file types in a brief mode using the file command in Linux. To do so you will need to use option -b. In this case, the output will not show the file names. In this example, I will display the types of files jobs.txt, file2.txt, and file1.txt in brief mode. You can follow the steps below to do the same.

Steps to Follow >

➊ Go to your Ubuntu Terminal.

➋ Write the given command in the command prompt:

file -b jobs.txt file2.txt file1.txt

➌ Now, press ENTER.

Output >

In the following image, you can see that I have displayed the desired file types in brief mode.Printing file types in a brief mode using the file command in Linux.


Similar Readings


Example 8: Displaying Mime Type of a File Using the “file” Command in Linux

You can display the mime type of a file using the file command in Linux with the option -i. The mime type is actually a string that enables the system to identify the type of a file. In this example, I will find out the mime type of the file “file2.txt”. You can do the same by following the steps below.

Steps to Follow >

➊ Launch the Ubuntu Terminal.

➋ Type the following command in the command prompt:

file -i file2.txt

➌ Hit the ENTER button.

Output >

In the given image, you can see that I have displayed the mime type of the desired file. Here, the mime type “text/plain” indicates that the file is a plain text file.displaying the mime type of a file.

Example 9: Printing Internal Debugging Information of a File Using the “file” Command in Linux

You can print the internal debugging information of a file using the file command in Linux with the option -d. In this example, I will view the debugging information of the file “file2.txt”. You can do the same by following the given process.

Steps to Follow >

➊ Open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

file -d file2.txt

➌ Press the ENTER button.

Output >

In the image below, you can see that I have displayed the debugging information of the desired file.Printing the internal debugging information of a file.

Conclusion

In this article, I have tried to present you with the frequent uses of the file command in Linux with practical examples. Using this command, you will be able to examine and learn about files available on your system. I hope the practical knowledge gained from this article will help you experience the command line and make you a power user of Linux.


Similar Readings

Rate this post
Anonnya Ghosh

Hello there! I am Anonnya Ghosh, a Computer Science and Engineering graduate from Ahsanullah University of Science and Technology (AUST). Currently, I am working as a Linux Content Developer Executive at SOFTEKO. The strong bond between Linux and cybersecurity drives me to explore this world of open-source architecture. I aspire to learn new things further and contribute to the field of CS with my experience. Read Full Bio

Leave a Comment