The file command comes with many Unix systems and has a database of signatures that it uses to identify the structure of an unknown file. The command can also be used against a directory to determine it as a directory. The syntax of the file command is very easy:
# file [file or directory]
Example:
# file /tmp /tmp: sticky, directory # file /tmp/adobegc.log /tmp/adobegc.log: ASCII text
It can also output if the file is empty, which can also com handy sometimes to find empty files. It also takes regex expression while providing the file or directory names.
file tests each argument in an attempt to classify it. There are three sets of tests, performed in this order: filesystem tests, magic tests, and language tests. The first test that succeeds causes the file type to be printed.
The type printed will usually contain one of the words text (the file contains only printing characters and a few common control characters and is probably safe to read on an ASCII terminal), executable (the file contains the result of compiling a program in a form understandable to some UNIX kernel or another), or data meaning anything else (data is usually ‘binary’ or non-printable).
file Command Examples
1. To get the file type:
# file file.txt
2. To get the file type in brief:
# file -b file.txt # file --brief file.txt
3. To compile a C code file:
# file -C file.c # file --compile file.c
4. To Cause a checking printout of the parsed form of the magic file:
# file -c file.txt # file --checking-printout file.txt
5. To Exclude the test named in testname:
# file -e text file.txt # file --exclude text file.txt
testname | Description |
---|---|
apptype | EMX application type (only on EMX). |
text | Various types of text files (this test will try to guess the text encoding, irrespective |
encoding | Different text encodings for soft magic tests. |
tokens | Looks for known tokens inside text files. |
cdf | Prints details of Compound Document Files. |
compress | Checks for, and looks inside, compressed files. |
elf | Prints ELF file details. |
soft | Consults magic files. |
tar | Examines tar files. |
6. To Use the specified string as the separator between the filename and the file result returned:
# file -F " " file.txt # file --separator " " file.txt
7. To Read the names of the files to be examined from namefile:
# file -f file.txt # file --files-from file.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