Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
The file command in Linux is used to determine the type of a given file. It examines the file content, rather than its extension, to classify the file as a text, executable, archive, image, etc. It is a vital tool for debugging, scripting, and ensuring files are handled correctly in various workflows. file works on virtually all file systems supported by Linux and is a go-to utility for system administrators, developers, and users working with mixed or unknown file types.
The file command is a robust utility that simplifies file type identification in Linux. Its versatility makes it indispensable for troubleshooting, scripting, and daily file management tasks. Share this guide with anyone needing a clear reference to maximize their efficiency with this powerful tool.
file [options] <file_name>
find
or xargs
to process a large number of files automatically.
find . -type f | xargs file
file * > report.txt
file -b example.txt
file -i image.jpg
file -f filelist.txt
file -L symlink
sudo file -s /dev/sda1
file --mime document.pdf
file -z archive.tar.gz
file --help
file -z archive.tar.gz
file --exclude=*.tmp *
While the file command itself doesn’t directly search inside files or compare their contents, it can be combined with other Linux utilities like grep
, cmp
, and diff
to create powerful workflows for searching, analyzing, and comparing files. Below, we outline practical techniques for these tasks, starting with content search and progressing to advanced comparisons.
You can see the full guide and cheat sheet here: Searching inside files and comparing their contents using Linux commands.
To determine the type of a single file:
file document.txt
To check the type of multiple files:
file file1 file2 file3
For determining the MIME type of files:
file -i video.mp4
To identify the target type of a symbolic link:
file -L symlink_to_binary
Use a file containing a list of filenames:
file -f filelist.txt
To identify the contents of a compressed file:
file -z logs.tar.gz
For identifying device file types:
sudo file -s /dev/sda1