CIS120Linux Fundementals
file Command
The file
command in Linux is used to determine the type of a file. This command helps users identify
files without relying solely on file extensions. The file
command examines the content of a file and
outputs a human-readable description of the file type.
Common Options for the file Command
The file
command comes with various options to refine its behavior. Below is a table of some of the most
common options and their descriptions:
Option | Description |
---|---|
-b |
Brief mode: do not prepend filenames to output lines |
-i |
Output MIME type strings rather than the traditional human-readable ones |
-f <name> |
Read the filenames to be examined from name instead of from the command line |
-z |
Try to look inside compressed files |
-L |
Follow symbolic links |
-s |
Read block or character special files |
-r |
Raw mode: do not translate unprintable characters to \ooo notation |
Examples
Here are some examples of how to use the file
command with different options, along with the expected
output:
Determining the File Type
This command checks the type of the file example.txt
.
file example.txt
Output
example.txt: ASCII text
Using the -b
Option (Brief Mode)
This command outputs only the file type without the filename.
file -b example.txt
Output
ASCII text
Using the -i
Option (MIME Type)
This command outputs the MIME type of the file example.txt
.
file -i example.txt
Output
example.txt: text/plain; charset=us-ascii
Checking Multiple Files
This command checks the types of multiple files.
file file1.txt file2.jpg file3.zip
Output
file1.txt: ASCII text
file2.jpg: JPEG image data, JFIF standard 1.01
file3.zip: Zip archive data, at least v2.0 to extract
Reading File Names from a File
Each line in filenames.txt
should contain the name of a file that actually exists in the directory where you run the command. For example:
file -f filenames.txt
Below is the contents of filenames.txt
document.pdf
image.png
script.sh
binaryfile
textfile.txt
directory/
Output
document.pdf: PDF document, version 1.4
image.png: PNG image data, 800 x 600, 8-bit/color RGB, non-interlaced
script.sh: Bourne-Again shell script, ASCII text executable
binaryfile: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux)
textfile.txt: ASCII text
directory/: directory
Explanation
file -f filenames.txt
reads each filename from the filefilenames.txt
and runs thefile
command on it.- The output describes the type of each file, just as if you had run
file filename
on each one individually. - If a filename in
filenames.txt
does not exist, an error message likefilename: cannot open 'filename' (No such file or directory)
will appear.
Looking Inside Compressed Files
This command attempts to look inside the compressed file compressed.tar.gz
and identify its contents.
file -z compressed.tar.gz
Output
compressed.tar.gz: gzip compressed data, was "compressed.tar", last modified: ...
Following Symbolic Links
This command follows the symbolic link symlink
and checks the type of the
file it points to. The output depends on the file type of the target. NOTE: We will be looking at symbolic links in Chapter 2
file -L symlink
Output
symlink: ASCII text
Conclusion
The file
command is a powerful tool for identifying the types of files in Linux. By understanding and
utilizing its various options, you can efficiently determine file types and gain insights into the nature of the
files you are working with. Practice using these options to become comfortable with the file
command
and enhance your file management skills in Linux.