我需要找到所有的图像文件从目录(gif, png, jpg, jpeg)。

find /path/to/ -name "*.jpg" > log

如何修改这个字符串,不仅找到。jpg文件?


当前回答

作为上面@Dennis Williamson回答的补充,如果你想让同一个regex对文件扩展名不区分大小写,使用-iregex:

find /path/to -iregex ".*\.\(jpg\|gif\|png\|jpeg\)" > log

其他回答

在Mac OS上使用

find -E packages  -regex ".*\.(jpg|gif|png|jpeg)"
find /path -type f \( -iname "*.jpg" -o -name "*.jpeg" -o -iname "*gif" \)

作为上面@Dennis Williamson回答的补充,如果你想让同一个regex对文件扩展名不区分大小写,使用-iregex:

find /path/to -iregex ".*\.\(jpg\|gif\|png\|jpeg\)" > log
find /path/to/ -type f -print0 | xargs -0 file | grep -i image

它使用file命令来尝试识别文件的类型,而不管文件名(或扩展名)。

如果/path/to或文件名包含字符串image,则上述操作可能返回虚假命中。那样的话,我建议

cd /path/to
find . -type f -print0 | xargs -0 file --mime-type | grep -i image/
find -regex ".*\.\(jpg\|gif\|png\|jpeg\)"