Ubuntu – Locate files matching mimetype in a directory recursively via command line

command linesearch

I wish to get a list of all files in a project folder [recursively] that are image files.
Unfortunately Google was unable to help me here.

If possible, the use of current system's mimetypes is preferred, eg all files that have a mime matching the glob image/*; so that any example could be easily extended for video/*, etc.

Additionally, commands that don't need to do expensive operations on every file in the entire directory are preferred as I might be trying to use this on some pretty huge volumes.


Note: A potential work around I have considered is fetching the list of file extensions from the mimetypes data and doing a find for these extensions, this is however less preferred due to the file extensions of the target files I'm wanting to locate may be missing, double loaded, etc.


Edit: Searching for files by extension, which I consider pretty trivial and not exactly what I'm looking for; I've edited the title to reflect that I'm looking specifically for mimetype based results.

Best Answer

Assuming you are on 14.04 (using python3), the small script below lists your files recursively in given directory. It identifies the file's mimetype by the file command, as described here

file --mime-type -b filename

additionally, you can extend the script by adding a command by using shutil (e.g. .move / .copy) at the same level as the print command.

Adding mimetypes

For a combined search, you can add (or remove) mimetypes to search for, by adding them to the filetypes -tuple.

The script

#!/usr/bin/env python3

import os
import subprocess

source_dir = "/path/to/directory"
filetypes = ("image", "video")

for root, dirs, files in os.walk(source_dir):
    for name in files:
        file = root+"/"+name
        ftype = subprocess.check_output(['file', '--mime-type', '-b', file]).decode('utf-8').strip()
        if ftype.split("/")[0] in filetypes:
            print(file)

How to use it

Copy the script into an empty file, set the directory to list (sourcedir) and the mimtype(s) to look for (filetypes), save it as list_files.py and run it by the command:

python3 /path/to/list_files.py