file Command in Linux
Overview
In Linux, the file command is used to determine the type of a file. It can identify files based on their contents rather than relying on the file extension. This makes it a useful tool for identifying unknown or suspicious files. In this article, we'll cover the syntax, options, and usage of the file command, along with some tips and advanced usages.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Linux file Command Syntax
The syntax for the file command is as follows:
Where:
- Options: The available options for the file command.
- Filename: The name of the file to be analyzed by the file command.
file Command Options:
- -b: Brief mode, displays the output in a compact format.
- -i: MIME mode, outputs the MIME type of the file.
- -z: Zero-terminated output, useful for scripting.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Example Usages
-
Display file type:
Output:
Explanation: This command displays the file type of 'myfile.txt' which is an ASCII text file.
-
Display file type in MIME format:
Output:
Explanation: This command displays the file type of 'myfile.jpg' in MIME format, which is an image/jpeg file.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Tips
-
The file command can be used to check if a file is corrupted or not.
-
The -b option can be used to display the output in a more readable format.
-
The file command works with any type of file, not just text files.
Advanced Use Cases of file Command in Linux
-
Display all files in a directory:
Output:
Explanation: This command displays the file types of all files in the current directory.
-
Identify shared libraries:
Output:
Explanation: This command identifies that '/usr/lib/libc.so.6' is a symbolic link to libc-2.28.so shared library.
-
Identify the architecture of an executable file:
Output:
Explanation: This command identifies the architecture of the executable file 'myprog' as 64-bit LSB, x86-64, and version 1 (SYSV). It also shows that the file is dynamically linked and not stripped of debug_info.
Conclusion
-
The file command is a powerful tool for identifying the type of a file based on its contents.
-
It can be used to identify unknown or suspicious files.
-
The command works with any type of file, not just text files.
-
The -b option can be used to display the output in a more readable format.
-
The -i option can be used to display the file type in MIME format.