Search code examples
bashmime-typessftp

MIME type check via SFTP connection


I want to list images by SFTP and save this list, so another script may further process it.

Unfortunately, there are also many other files there, so I need to identify which are images. I am filtering out everything with wrong file extension, but I would like to go a step further and check also the content of the file.

Downloading everything to check it with file --mime-type on local machine is too slow. Is there a way how to check MIME type of a file on remote SFTP before the download?


Solution

  • We found a way, downloading only first 64 bytes. It is a lot faster than downloading whole file, but still enough to see if it looks like an image:

    curl "sftp://sftp.example.com/path/to/file.png" -u login:pass -o img.tmp -r 0-64
    file --mime-type img.tmp