The Linux File Command: How to Use It to Determine a File Type (2024)

VPS

Dec 22, 2023

Edward S. & Noviantika G.

6min Read

The Linux File Command: How to Use It to Determine a File Type (1)

In UNIX-like systems, file names can be entirely different from their actual types. In some cases, they don’t even have valid extensions. Therefore, it can make managing data more complicated.

To organize information quickly, Linux provides a program called the file command. It’s primarily used to determine the file type – either American Standard Code for Information Interchange (ASCII) text or Multipurpose Internet Mail Extensions (MIME) format.

In this tutorial, you’ll learn the basics of using the program and how it can empower your server management and Linux operation skills.

The Linux File Command: How to Use It to Determine a File Type (2)

Linux File Command Syntax

To use the Linux file command on a VPS hosting, you’ll have to connect it with an SSH client such as PuTTY or Terminal.

First, let’s analyze the basic syntax of the file command:

file [options] [file name]

  • file – instructs the shell to execute the file command.
  • [options] – modifies the command’s operation.
  • [file name] – inserts the file name you want to inspect.

When executed, the command doesn’t consider its file extension. Instead, it runs three tests to determine the file type:

  • Filesystem test – examines the return from the stat system call. The program reviews if it is an empty file or a special file type. It also looks for known formats relevant to the system you work on if they’re specified in the system header file.
  • Magic test – uses “magic numbers,” a short string of numbers at the beginning of a file, to check whether it is binary executable data. The necessary information to run this test is available in /etc/magic or /usr/share/misc/magic from a compiled magic file.
  • Language test – examines the character sets the file is written in, such as ASCII text or UTF-8. It looks for any special sequence that appears in the first few lines. This test is less accurate, so it’s performed last.

The command’s output displays the file type using the standard format. Depending on the command option, it may provide other information, such as data stored in compressed files, size, or version.

The option in the syntax allows you to add variables to the Linux file command. Here are some common examples:

  • -b or –brief – fetches a short description of the file type.
  • file * – lists all file types in the current working directory.
  • -i or –mime – shows the MIME file type.
  • -s or –special-files – reads special files.
  • -z or –uncompress – checks and displays information inside compressed files.
  • -c or –checking-printout – checks a magic file’s parsed version.
  • -m or –magic-file – utilizes an alternative magic file provided by the user.
  • -d – displays internal debugging information using the standard format.
  • <regex range> – fetches file types in specific ranges.
  • -0 or –print0 – prints a null character at the end of the file name.
  • –help – shows the file command’s help message. It also lists acceptable options and their usage.

Before we talk about each option separately, use the nano editor to create a sample text named test.txt:

nano test.txt

Once the command line opens a new file, write a few lines of text and press Ctrl + X and Y to exit and save your changes.

Suggested Reading

40 Essential Linux Commands That Every User Should Know

Linux File Command Examples

In the following sections, we’ll discuss how to use each of the options listed previously.

Use File to Check the File Type

In Linux, while users can rename their files, the updated information may not represent the actual data. To find the correct type of a file, enter:

file filename

For example, you rename test.txt to text.zip. To reveal the valid file type, enter:

file text.zip

The output will display the name and its actual type, an ASCII text file:

To view the format in brief mode, use the -b option on Terminal, followed by the file name. For example:

file -b text.zip

The output will show the file type without its name:

Use File to List the File Type of Multiple Files

The file command can list each file type in the home directory. To do this, enter file and add a wildcard character (*):

file *

The program will show all the files and directories:

In addition, the file command can show each file type inside a specific directory. Here’s the general syntax:

file [path-to-directory]/*

Use File to Find the MIME File Type

The -i option is used to view the MIME file type. It consists of two parts – a type and a subtype. MIME uses a slash (/) to separate each of them, with no space in between.

Here’s the general syntax:

file -i filename

For example, to view the MIME type of the test2.txt file, enter:

file -i test2.txt

Here’s the output of the file command above:

Instead of declaring the file format as ASCII text, the program defines the file as text/plain and charset=us-ascii.

Use File to Read the Special File Type

The file command allows you to read special files, such as system information, by adding the -s option.

Important! Keep in mind that only a root user can run the file command along the -s option. Otherwise, you’ll receive a no-read permission error message.

This option only classifies a file as a block special file, symbolic link, directory, or nonexistent.

Here’s its general format:

sudo file -s filename

For example, to read the ploop19269 file, enter:

sudo file -s /dev/ploop19269

The output indicates that ploop19269 is a DOS/MBR boot sector.

Use File to Read a Compressed File

There are two ways to check compressed files like ZIP or gzip archives, the -z and -Z options. The former displays detailed information and its content, while the latter only shows the file types.

Here’s the general syntax of the -z option:

file -z filename

For example, to read the test2.txt.gz file’s complete data, enter:

file -z test2.txt.gz

The output specifies that test2.txt.gz is a gzip compressed file that contains test2.txt:

Here’s the general format of the -Z option:

file -Z filename

For example, to view the file type of test.gz only, enter:

file -Z test.gz

This command will only print out the type of the file inside test.gz – an ASCII text.

Use File to Test Parsed Version of a File

Adding the -c option allows you to view the parsed version of any file. It shows information such as type, opcode, and value. Usually, it is used in conjugation with the -m option to debug a new magic file before installing it.

Here’s its general syntax:

file -c filename

For example, to print the parsed form of the test.txt file, enter:

file -c test.txt

The output should look like this:

Use File to List File Types

The file command lists all file types in a directory using the Regex-style ranges. Type file and place the values in brackets, followed by *.

Its general syntax is:

file [range1-range2]*

For example, to examine files starting within the a to z range, enter:

file [a-z]*

The output should look like this:

Since this program is case-sensitive, the output will only show the files starting with a lowercase a to z. To include the uppercase characters, add another range. For example:

file [a-z]* [A-Z]*

Here’s what the output looks like:

Conclusion

In UNIX systems, file names and extensions can differ from their actual types. Hence, Linux provides the file command to help users determine the type of a file.

When executing it, use appropriate options and specify the file name. There are many acceptable variables to use with the file command, such as:

  • -c – tests the parsed form of a file.
  • -i – finds the mime type.
  • * – lists multiple files.
  • -z – reads compressed content.

We hope this article has helped you learn how to manage data using the Linux file command. If you have any questions or suggestions, please leave them in the comments section below.

Learn More Linux Commands for File Management

Remove Directory in Linux: How to Delete Files and Folders
How to Use the Linux Locate Command to Find Any File
Using Tee Command to Write to Files
How to Use the Tar Command in Linux
How to Rename a File
How to Create Linux Symlinks (Symbolic Links) for Files and Directories
How to Install and Use Linux Screen
How to Use rsync Command to Transfer and Sync Files
How to Read a File With Sed Command
How to Unzip Files in Linux
How to Use SCP Command to Copy and Transfer Files in Linux

Linux File Command FAQ

In this section, we will answer the most common questions about the Linux file command.

What Exactly Does the Linux File Command Do?

File names in UNIX can be entirely independent of the file types. Thus, it’s tricky to determine the actual information.

Executing the file command reveals what format a file uses and examines each argument by conducting three tests – filesystem, magic, and language tests. The first that succeeds will output the file type.

Which Linux Command Creates a Blank File in the Current Directory?

To create one or multiple empty files, use the touch command. It comes with the Linux system and is especially useful when you don’t have data to store at the time.

Its general syntax is: touch filename. To create multiple files, enter: touch filename1 filename2.

The Linux File Command: How to Use It to Determine a File Type (14)

The author

Edward S.

Edward is a content editor with years of experience in IT writing, marketing, and Linux system administration. His goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.

More from Edward S.

The Linux File Command: How to Use It to Determine a File Type (15)

The Co-author

Noviantika G.

Noviantika is a web development enthusiast with customer obsession at heart. Linux commands and web hosting are like music to her ears. When she's not writing, Noviantika likes to snuggle with her cats and brew some coffee.

More from Noviantika G.

The Linux File Command: How to Use It to Determine a File Type (2024)
Top Articles
Why CFD Trading is Banned in the USA: Understanding the Regulatory Landscape
Device ID String - Windows drivers
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Saturnina Altenwerth DVM

Last Updated:

Views: 6369

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Saturnina Altenwerth DVM

Birthday: 1992-08-21

Address: Apt. 237 662 Haag Mills, East Verenaport, MO 57071-5493

Phone: +331850833384

Job: District Real-Estate Architect

Hobby: Skateboarding, Taxidermy, Air sports, Painting, Knife making, Letterboxing, Inline skating

Introduction: My name is Saturnina Altenwerth DVM, I am a witty, perfect, combative, beautiful, determined, fancy, determined person who loves writing and wants to share my knowledge and understanding with you.