Linux File Command: How to Determine File Type in Linux {+10 Examples} (2024)

Introduction

The Linux file command helps determine the type of a file and its data. The command doesn't take the file extension into account, and instead runs a series of tests to discover the type of file data.

In this tutorial, we will show you how the file command works and how to use it.

Linux File Command: How to Determine File Type in Linux {+10 Examples} (1)

Prerequisites

  • A system running Linux.
  • Access to the terminal window.

File Command Syntax

The file command uses the following basic syntax:

file [option] [file name]

In the syntax above, file name represents the name of the file you want to test. The file command performs three sets of tests trying to determine the file type, in this order:

  • Filesystem tests perform a stat(2) system call and check the result against the system header file. This way, the file command determines if the file is a common type for your system (such as a text file, image, directory, etc.).
  • Magic tests use a short string of numbers stored near the beginning of the file ("magic number") to test if the file is a binary executable and, if so, determine its type. The information needed to perform these tests is stored in /etc/magic or /usr/share/misc/magic from a compiled magic file.
  • Language tests use a special tag to determine which programming language a binary executable file is written in.

The command output provides a short description of the file and data type. For instance, using the file command to test a text file:

file example.txt
Linux File Command: How to Determine File Type in Linux {+10 Examples} (2)

File Command Options

The file command uses the following options:

OptionDescription
--appleChanges the command output to the one used by older versions of MacOS.
-b, --briefChanges command output to brief mode.
-C, --compileCreates an output file that contains a pre-parsed version of the magic file or directory.
-c, --checking-printoutChecks the printout for the parsed version of the magic file.
-dPrints internal debugging information in the standard error format.
-EOn filesystem error, issues an error message and exits.
-e, --excludeExcludes a test from the list of tests performed on a file.
--exclude-quietExcludes tests that the file command doesn't know about.
--extensionPrints a list of valid extensions for the file type.
-F, --separatorUses the provided string as a separator between the file name and file type.
-f, --files-fromUses a provided text file as a list of files to test. The list must contain only one file name per line.
-h, --no-deferenceDisables following symbolic links.
-i, --mimeChanges the command output to a MIME-type string.
--mime-type, --mime-encodingChanges the command output to a MIME-type string and only displays the specified element (type or encoding).
-k, --keep-goingKeeps the test going after the first results match.
-l, --listShows a list of matching patterns in descending order of strength.
-L, --deferenceEnables following symbolic links.
-m, --magic-fileUses an alternative magic file provided by the user.
-N, --no-padDoesn't pad the file names to align with the output.
-n, --no-bufferFlushes the output after checking each file.
-p, --preserve-dateAttempts to preserve the last time the file was accessed to make it look like the file command didn't test it.
-P, --parameterSets various parameters, such as max bytes or recursion, count, and length limit.
-r, --rawDisables translating unprintable characters.
-s, --special-filesEnables reading special files.
-S, --no-sandboxDisables sandboxing on systems that support it.
-v, --versionDisplays the version of the file command.
-z, --uncompressChecks compressed files.
-Z, --uncompress-noreportChecks compressed files and only displays file type without the compression.
-0, --print0Displays a null character after the end of the file name.
--helpDisplays the help message.

File Command Examples

Below are examples of common use cases for the file command.

Test Multiple Files

To test multiple files simultaneously, append the file names to the end of the file command:

file [file name 1] [file name 2] … [file name n]

For instance, testing a directory, a text file, an image, and a webpage:

file Example example.txt sample.png index.html
Linux File Command: How to Determine File Type in Linux {+10 Examples} (3)

Test All Files in a Directory

Add a wildcard character (*) to the file command to test all the files and directories in the current working directory:

file *
Linux File Command: How to Determine File Type in Linux {+10 Examples} (4)

Add the path to a directory to the wildcard character to test the contents of that directory:

file [path to directory]/*

For instance, to test the contents of the Example directory, use:

file Example/*
Linux File Command: How to Determine File Type in Linux {+10 Examples} (5)

Test Files in a Range

The file command lets you test a subset of files in a directory using Regex-style ranges. Select a range by placing the values in brackets. For instance, testing files and directories with names in the a-l range:

file [a-l]*
Linux File Command: How to Determine File Type in Linux {+10 Examples} (6)

Since Regex-style ranges are case-sensitive, the output in the example above only shows file types for files starting with lowercase a-l. Adding another range lets you include uppercase characters as well:

file [a-l]* [A-L]*
Linux File Command: How to Determine File Type in Linux {+10 Examples} (7)

Test Files from a List

The file command lets you use a text file as a list of files to test. The text file must only contain one file name per line.

Linux File Command: How to Determine File Type in Linux {+10 Examples} (8)

Use the -f option and add the path to the list file to file command:

file -f list.txt
Linux File Command: How to Determine File Type in Linux {+10 Examples} (9)

Test Special Files

The file command is not always able to read special files, such as system files:

file /dev/sda5
Linux File Command: How to Determine File Type in Linux {+10 Examples} (10)

In the example above, the file command output shows that /dev/sda5 is a block special file but doesn't offer additional details. Using the -s option lets you fully test special files:

sudo file -s /dev/sda5
Linux File Command: How to Determine File Type in Linux {+10 Examples} (11)

Note: If you are not logged in as the root user, attempting to test a special file results in a no read permission error message. Add sudo to the file command to prevent this.

Test Compressed Files

Use the -z option to fully test compressed files, trying to detect their content:

file -z install.tar.gz
Linux File Command: How to Determine File Type in Linux {+10 Examples} (12)

Test Parsed Version of File

Using the -c option displays the check printout for the parsed version of the file:

file -c example.txt
Linux File Command: How to Determine File Type in Linux {+10 Examples} (13)

This option is usually used with the -m option to debug and install a new magic file.

Display Brief Output

Using the -b option displays the brief version of the output. This version of the output only shows file types and omits file names.

file -b Example example.txt sample.png index.html
Linux File Command: How to Determine File Type in Linux {+10 Examples} (14)

Add Separators to Output

Using the -F option lets you define a character that acts as a separator between the file name and file type sections of the output. For instance, add a plus sign (+) as a separator with:

file -F + Example example.txt sample.png index.html
Linux File Command: How to Determine File Type in Linux {+10 Examples} (15)

Remove File Name Padding from Output

Use the -N option to remove the padding between the file name and file type sections of the output:

file -N Example example.txt sample.png index.html

The resulting output is no longer vertically aligned:

Linux File Command: How to Determine File Type in Linux {+10 Examples} (16)

Conclusion

After reading this tutorial, you should be able to use the Linux file command to find out the types of files on your system.

If you're interested in learning more about Linux commands, have a look at our Linux commands cheat sheet.

Linux File Command: How to Determine File Type in Linux {+10 Examples} (2024)
Top Articles
30 Ways To Save Money & Cut Down Your Monthly Expenses
Can You Have Two Car Insurance Policies? Everything You Need To Know
Methstreams Boxing Stream
Www.politicser.com Pepperboy News
Phone Number For Walmart Automotive Department
THE 10 BEST Women's Retreats in Germany for September 2024
Beds From Rent-A-Center
Crime Scene Photos West Memphis Three
Carter Joseph Hopf
Dark Souls 2 Soft Cap
Revitalising marine ecosystems: D-Shape’s innovative 3D-printed reef restoration solution - StartmeupHK
Craigslist Cars Nwi
6th gen chevy camaro forumCamaro ZL1 Z28 SS LT Camaro forums, news, blog, reviews, wallpapers, pricing – Camaro5.com
Restaurants Near Paramount Theater Cedar Rapids
Five Day National Weather Forecast
Swedestats
8664751911
Craigslist Mt Pleasant Sc
Ratchet & Clank Future: Tools of Destruction
Caledonia - a simple love song to Scotland
Winco Employee Handbook 2022
Providence Medical Group-West Hills Primary Care
Ac-15 Gungeon
Www.dunkinbaskinrunsonyou.con
Does Hunter Schafer Have A Dick
Chime Ssi Payment 2023
Turbo Tenant Renter Login
Cb2 South Coast Plaza
At 25 Years, Understanding The Longevity Of Craigslist
Panolian Batesville Ms Obituaries 2022
No Limit Telegram Channel
208000 Yen To Usd
Table To Formula Calculator
Weather Underground Durham
The Posturepedic Difference | Sealy New Zealand
County Cricket Championship, day one - scores, radio commentary & live text
Craigslist Central Il
Amici Pizza Los Alamitos
Metro 72 Hour Extension 2022
Reborn Rich Ep 12 Eng Sub
Oriellys Tooele
Red Dead Redemption 2 Legendary Fish Locations Guide (“A Fisher of Fish”)
Silive Obituary
התחבר/י או הירשם/הירשמי כדי לראות.
Rocket Lab hiring Integration & Test Engineer I/II in Long Beach, CA | LinkedIn
Aznchikz
Used Auto Parts in Houston 77013 | LKQ Pick Your Part
Rocket Bot Royale Unblocked Games 66
Coleman Funeral Home Olive Branch Ms Obituaries
Buildapc Deals
Where To Find Mega Ring In Pokemon Radical Red
Lorcin 380 10 Round Clip
Latest Posts
Article information

Author: Laurine Ryan

Last Updated:

Views: 6640

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Laurine Ryan

Birthday: 1994-12-23

Address: Suite 751 871 Lissette Throughway, West Kittie, NH 41603

Phone: +2366831109631

Job: Sales Producer

Hobby: Creative writing, Motor sports, Do it yourself, Skateboarding, Coffee roasting, Calligraphy, Stand-up comedy

Introduction: My name is Laurine Ryan, I am a adorable, fair, graceful, spotless, gorgeous, homely, cooperative person who loves writing and wants to share my knowledge and understanding with you.