Linux Commands - file command (determine file type)
by
Jeremy Canfield |
Updated: December 30 2022
| Linux Commands articles
The file command can be used to display a file type. Let's create an empty file.
touch foo.txt
The file command will show that foo.txt is an empty file.
~]# file foo.txt
foo.txt: empty
Let's create a directory.
mkdir bar
The file command will show that bar is a directory.
~]# file bar
bar: directory
Let's append text to foo.txt.
echo "Hello World" > foo.txt
Now the file command should return the following.
~]# file foo.txt
foo.txt: ASCII text
XML files should return XML <version> document text.
~]$ file example.xml
example.xml: XML 1.0 document text
Let's say you have a certificate file, foo.cer, that contains something like this.
-----BEGIN CERTIFICATE-----
MIIGhTCCBW2gAwIBAgITFwAS0Zj4+uylATknJgAAABLRmDANBgkqhkiG9w0BAQsF
ADBMMRQwEgYKCZImiZPyLGQBGRYEY29ycDEYMBYGCgmSJomT8ixkARkWCHRocml2
ysR4VfQLr+A3zbM59CQjewP40y7oFgrpNuj8Hp1AXud3nsakEYFaGcc=
-----END CERTIFICATE-----
Something like this should be returned.
foo.cer: PEM certificate
Let's say you have a binary file foo.file. Attempting to view the content of foo.file will probably return mumbo jumbo, something like this.
~]# cat foo.file
h++-://c_+.+h_i+e++.c-+/ce_+e+_-+
The file command should identify foo.file as a data file.
~]# file foo.file
foo.file: data
Did you find this article helpful?
If so, consider buying me a coffee over at