I'm afraid Schrödinger's cat.
It is not possible to determine the contents of a file without opening it. The file system does not contain metadata related to the content.
If you do not open the file, this is not a strict requirement, then there are a number of solutions available to you.
Edit:
A number of comments and answers suggested that file(1) is a good way to define content. Indeed. However, file(1) opens a file that was forbidden in the question. See the penultimate line in the following example:
> echo 'This is not a pipe' > file.jpg && strace file file.jpg 2>&1 | grep file.jpg execve("/usr/bin/file", ["file", "file.jpg"], [/* 56 vars */]) = 0 lstat64("file.jpg", {st_mode=S_IFREG|0644, st_size=19, ...}) = 0 stat64("file.jpg", {st_mode=S_IFREG|0644, st_size=19, ...}) = 0 open("file.jpg", O_RDONLY|O_LARGEFILE) = 3 write(1, "file.jpg: ASCII text\n", 21file.jpg: ASCII text
Johnsyweb
source share