how to use file command with its examples

This command is used to determine the “type” of a file present under system. When run the command with the file name, it checks the file’s contents and gives details about its type and other details.

  • It comes from “file-x.x” package.
  • Configuration Files are:   
                /usr/share/misc/magic.mgc   Default compiled list of magic.
          /usr/share/misc/magic            Directory containing default magic files.


1. To get the file type

$ file test.txt

2. To get the file type in brief

$ file -b test.txt
$ file –brief test.txt 

3. To compile a C code file

$ file -C test.txt
$ file –compile text.txt 

4. To Cause a checking printout of the parsed form of the magic file

$ file -c test.txt
$ file –checking-printout test.txt 

5. To Exclude the test named in testname

$ file -e text file.txt
$ file –exclude text file.txt

6. To Use the specified string as the separator between the filename and the file result returned

$ file -F ” ” text.txt 
$ file –separator ” ” text.txt 

7. To Read the names of the files to be examined from namefile

$ file -f text.txt
$ file –files-from text.txt

8. To cause symlinks not to be followed

$ file -h file.txt
$ file –no-dereference file.txt 

9. To causes the file command to output mime type strings

$ file -i file.txt
$ file -mime file.txt 

10. Like -i, but print only the specified element

$ file –mime-type file.txt
$ file –mime-encoding file.txt 

11. To Dont stop at the first match, keep going

$ file -k file.txt
$ file –keep-going file.txt 

12. To option causes symlinks to be followed

$ file -L file.txt
$ file –dereference file.txt 

13. To specify an alternate list of files and directories containing magic.

$ file -m magicfile file.txt
$ file –magic-file magicfile file.txt 

14. To Don’t pad filenames so that they align in the output.

$ file -N file.txt
$ file –no-pad file.txt 

15. To Force stdout to be flushed after checking each file.

$ file -n file.txt
$ file –no-buffer file.txt 

16. To preserve the access time of files

$ file -p file.txt
$ file –preserve-date file.txt 

17. Don’t translate unprintable characters to ooo

$ file -r file.txt
$ file –raw file.txt 

18. To reading special files

$ file -s file.txt
$ file –special-files file.txt 

19. To Print the version of the program and exit.

$ file -v  

20. To Try to look inside compressed files.

$ file -z file.gz
$ file –uncompress file.gz 

21. To Output a null chaacter 0 after the end of the filename

$ file -0 file.txt
$ file –print0 file.txt 

22. To get the help for file

$ file –help

Related Commands: magic, strings, od, hexdump, file 

Leave a Reply

Your email address will not be published. Required fields are marked *