Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash, getting the mime type of a file handle

How would i get the mime type of a file-handle without saving it to disk?

What i mean is a file that is not saved to disk, rather: i extracted it from an archive and plan on piping it to another script.

Say i extracted the file like this:

tar -xOzf images.tar.gz images/logo.jpg | myscript

Now inside myscript I would like to check the mime type of the file before further processing it. How would it go about this?

like image 880
dovidweisz Avatar asked Nov 16 '25 08:11

dovidweisz


1 Answers

as some people think my comment above is helpful i post it as an answer. the file-command is able to determine a file's mime type on the fly/when being piped. It ist able to read a file from stdin - printing the file's --mime-type briefly/in a short manner when passing -b. Considering your example you probably want to extract a single file from an archive and dertermine its file/mime type.

$ tar -xOzf foo.tar.gz file_in_archive.txt | file -b --mime-type -
text/plain

so for a simple text file extracted from an archive to stdout it could look like the example above. hope that helped. regards

like image 107
Marc Bredt Avatar answered Nov 19 '25 11:11

Marc Bredt