The Wayback Machine - https://web.archive.org/web/20240723042730/https://www.geeksforgeeks.org/file-command-in-linux-with-examples/
Open In App

file command in Linux with examples

Last Updated : 19 Feb, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

file command is used to determine the type of a file. .file type may be of human-readable(e.g. ‘ASCII text’) or MIME type(e.g. ‘text/plain; charset=us-ascii’). This command tests each argument in an attempt to categorize it.

It has three sets of tests as follows:

  • filesystem test: This test is based on the result which returns from a stat system call. The program verifies that if the file is empty, or if it’s some sort of special file. This test causes the file type to be printed.
  • magic test: These tests are used to check for files with data in particular fixed formats.
  • language test: This test search for particular strings which can appear anywhere in the first few blocks of a file.

Syntax:

file [option] [filename]

Example: Command displays the file type

file email.py
file name.jpeg
file Invoice.pdf
file exam.ods
file videosong.mp4

file command

Options:

  • -b, –brief : This is used to display just file type in brief mode.

    Syntax:

    file -b filename
    

    Example:

    file -b email.py
    file -b input.txt
    file -b os.pdf
    

    file command

    Here, we can see that file type without filename.

  • * option : Command displays the all files’s file type.
    file *
    

    file command

    The output shows all files in the home directory

  • directoryname/* option : This is used to display all files filetypes in particular directory.

    Syntax:

    file directoryname/*
    

    Example:

    file work/*
    

    file command

    The output shows all files in a particular directory.

  • [range]* option: To display the file type of files in specific range.

    Syntax:

    file [range]*
    

    Example:

    file [a-z]*
    file [a-e]*
    

    file command

    The output shows the range of files.

  • -c option: Cause a checking printout of the parsed form of the magic file. This option is usually used in conjunction with the -m flag to debug a new magic file before installing it.
    file -c
    

    Example:

    file -c
    

    file command

  • -f option: Read the names of the files to be examined from namefile (one per line) before the argument list. Either namefile or atleast one filename argument must be present; to test the standard input, use ‘-’ as a filename argument.

    Syntax:

    file -f -
    

    file command

  • -F option : File and file type are separated by :. But we can change separator using -F option

    Syntax:

    file -F "-" filename
    

    Example:

    file -F - input.txt
    file -F + os.pdf
    

    file command

    The output shows file and file types are separated by – and +.

  • -i option: To view mime type of file.

    Syntax:

    file -i filename
    

    Example:

    file -i input.txt
    file -i os.pdf
    

    file command

  • -N option: Don’t pad filenames so that they align in the output.
    file -N *
    

    Example:

    file -N *
    

    file command

  • -s option: For special files

    Syntax:

    file -s filename
    

    Example:

    file /dev/sda  
    file -s /dev/sda  
    file /dev/sda5  
    file -s /dev/sda5 
    

    file command

  • filenames: Displays file types of multiple files

    Syntax:

    file filenames
    

    Example:

    file input.txt .local Desktop
    

    file command

  • -z option: Try to look inside compressed files.

    Example:

    file -z flash.tar.gz
    

    file command

  • –help option: Print a help message and exit.

    file command
    file command


Previous Article
Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
How to Save Output of Command in a File in Linux?
When we run a command on a terminal on Linux it generates some output of that command. Sometimes we need the output result of the commands. Today we are going to see how to save the output of the command. Using Redirections in Linux: We can use the redirections operators to save the output of commands into files. Redirection operators redirect the
3 min read
How to Create a Text File Using the Command Line in Linux
There are a couple of quick ways to create a text file from the Linux Command Line or Terminal. Some of them have been explained in the following article. 1) touch commandThis is the most standard command to quickly create an empty text file. The command is quite simple to type and makes it quite easier to create several text files at once. The com
6 min read
Extract Emails From a Text File Using Grep Command in Linux
Let's consider we have a text file that contains lots of text and in that text file there are some email IDs present, and we have to find the all email IDs present in that text file. So what we can do? How can we find all email IDs present in that text file?. One way to find all email IDs manually, but this is a very time-consuming and boring proce
3 min read
How to compress file in Linux | Compress Command
Linux, renowned for its powerful command-line utilities, provides users with various tools to streamline file management tasks. One such tool, the compress `command`, is specifically designed to compress individual files, reducing their size for efficient storage and transfer. In this comprehensive guide, we'll explore the ins and outs of the compr
5 min read
How to Mount File System in Linux | mount Command
All files in a Linux filesystem are arranged in form of a big tree rooted at '/'.These files can be spread out on various devices based on your partition table, initially your parent directory is mounted(i.e attached) to this tree at '/', others can be mounted manually using GUI interface(if available) or using mount command. mount command is used
6 min read
Use the Lines of a File as Arguments of a Linux Command
Handling multiple arguments for a command can be difficult, especially when there are a lot of parameters. One way to make this easier is to put the arguments in a text file and use each line of the file as an argument for the command. This can simplify the process and save time. For example, let's say you have a text file with a list of 100 client
6 min read
How to Move File in Linux | mv Command
The `mv` command in Linux is like a superhero tool that can do a bunch of cool stuff with your files and folders. Think of it as a digital moving truck that helps you shift things around in your computer. Whether you want to tidy up your folders, give your files new names, or send them to different places, `mv` is the go-to friend for the job. It's
7 min read
Unmount File System in Linux Using the umount Command
Unmounting a file system is a vital procedure for securely deleting storage devices or rendering system resources accessible. A vital instrument in this procedure is the "umount" command, which enables users to softly unmount mounted devices or partitions. Before beginning the unmounting process, this step ensures data integrity and protects any po
7 min read
Article Tags :
three90RightbarBannerImg