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 hereadditionally, you can extend the script by adding a command by using
shutil
(e.g..move / .copy
) at the same level as theprint
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
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 aslist_files.py
and run it by the command: