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

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

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


当前回答

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 /path/to -regex ".*\.\(jpg\|gif\|png\|jpeg\)" > log

find -E /path/to -regex ".*\.(jpg|gif|png|jpeg)">日志

-E使您不必转义正则表达式中的paren和管道。

在Mac OS上使用

find -E packages  -regex ".*\.(jpg|gif|png|jpeg)"
find /path/to/  \( -iname '*.gif' -o -iname '*.jpg' \) -print0

将工作。也许有更优雅的方式。

添加-regextype posix-extended选项只在我的情况下有效:

sudo find . -regextype posix-extended -regex ".*\.(css|js|jpg|jpeg|png|ico|ttf|woff|svg)" -exec chmod 0640 {} \;