file command in Linux with examples

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : shubham_singh



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.