Exclude images from a Linux find

If you want to find all files in a sub directory and you don't want images to show up in the list here is a way to do it:

[local_username@localhost /]$ find /some/dir/ -type f \( -name '*.png' -o -name '*.jpg' -o -name '*.jpeg' -o -name '*.gif' \) -prune -o -exec stat --format='%n' {} \;

Explanation:
find - the command Linux uses to find files recursively
/some/dir/ - the directory you want to search
-type f - search only files of type f that is to say search files and not directories
\( - start list of options to skip
-name '*.png' - match any file of the form *.png (example.png)
-o - or
-name '*.jpg' -o -name '*.jpeg' -o -name '*.gif' - other common image extensions
\) - end list of options to skip
-prune - remove anything that meets the previous options
-o -exec - run the following example command against the file
stat --format='%n' - get the files name (this is here just as an example, you could put what ever options you want here)
{} - the file name from find
\; - marks the end of the -exec option

comments powered by Disqus