How to apply the linux 'file' command to the output (STDOUT) of a program to determine its file type

31 Views Asked by At

I want to determine the file-type of the contents of the STDOUT output of a program (without first saving the output to a file) i.e., notionally, I would like to run: myprogram | file

However, `/usr/bin/file can't read from STDIN.

If I try something like: my program | file <(cat /dev/stdin), I get the error broken symbolic link to pipe:[460261257]

I know I could save the output to a temp file, run file on the temp file and then erase the temp file but that seems very kludgy.

So is there any bash magic with redirection and/or file descriptors that will allow me to determine the file-type of the output of a program?

2

There are 2 best solutions below

1
MeetTitan On

Use the - file name to specify to file you want to read from stdin

ex. cat myfile | file -

0
Barmar On

Use command substitution:

file <(myprogram)

This might not work if file needs to seek in the file, since the argument will be a named pipe.