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: @KasiyA pointed out a thread exemplifying 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.

  • 1
    @KasiyA Thank you for pointing that out, I do not believe my target results are the same due to my desire to not rely on file extensions as a way to determine is a file "is an image", or is any other type. – ThorSummoner Sep 10 '14 at 18:58

I am not good in scripting but still this can find all files recursively and display it which are of image type using mimetype of files .

 find . -print0 | while IFS= read -r -d'' f; do 
    file -ib $f | grep "^image/" && echo $f; 

This might be close:

find . -type f -exec mimetype {} + | awk -F': +' '{ if ($2 ~ /^image\//) print $1 }'

The regex being matched in the awk expression can be easily extended.


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:

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
  • 1
    Why use file if there's Python library to do it? stackoverflow.com/q/14412211 – muru Sep 13 '14 at 14:32
  • @muru I tried that, also docs.python.org/2/library/mimetypes.html, but got quite a few exceptions on a large directory I tested it on. Using file turned out to be pretty water-tight. – Jacob Vlijm Sep 13 '14 at 14:39
  • @muru: mimetypes doesn't look at the content (only the filename extension). You might mean imghdr or python-magic instead e.g., for curpath, _, files in os.walk(os.curdir): for path in (os.path.join(curpath, name) for name in files): if re.match('image|video', magic.from_file(path, mime=True)): print(path) – jfs Aug 15 '17 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.