I wanted to find the number of image files within a directory. I ended up using this :
find . -type f -exec file {} \; | grep -c -i 'image'
This feels like an inefficient way of doing it. I could think of another way to do it - using an OR statement, but I couldn't figure out from the man page how this could be done.
Any ideas how this could be done more efficiently? I don't want to execute the commands above on a directory with a rather large number of files and have it take forever.
If you want to search for images by extension, you can use the following command:
Of course, you need to list all extensions you are interested in.
To get the count, you can append
| wc -l
.You can get an efficiency improvement by using
\+
instead of\;
. The difference here is that\+
passes all found files to a single execution offile
where\;
executesfile
once for every file that was found.You will run into problems if it finds more files than
file
can take as arguments. You could pipe it throughxargs
with the--max-args
option if that is the case.if you want to find files by mime-type of file in this case "image", you will need following command