我需要从目录(gif,png,jpg,jpeg)中找到所有图像文件 .
find /path/to/ -name "*.jpg" > log
如何修改此字符串以查找不仅仅是.jpg文件?
find /path/to/ -type f -print0 | xargs -0 file | grep -i image
这使用 file 命令尝试识别文件类型,无论文件名(或扩展名)如何 .
file
如果 /path/to 或文件名包含字符串 image ,则上述内容可能会返回虚假命中 . 在那种情况下,我建议
/path/to
image
cd /path/to find . -type f -print0 | xargs -0 file --mime-type | grep -i image/
find -regex ".*\.\(jpg\|gif\|png\|jpeg\)"
find /path -type f \( -iname "*.jpg" -o -name "*.jpeg" -o -iname "*gif" \)
find /path/to -regex ".*\.\(jpg\|gif\|png\|jpeg\)" > log
find /path/to/ -iname '*.gif' -o -iname '*.jpg' -o -iname '*.png' -o -iname '*.jpeg'
将工作 . 可能会有更优雅的方式 .
作为@Dennis Williamson上面的回复的补充,如果你想让相同的正则表达式对文件扩展名不区分大小写,请使用-iregex:
find /path/to -iregex ".*\.\(jpg\|gif\|png\|jpeg\)" > log
在Mac OS上使用
find -E packages -regex ".*\.(jpg|gif|png|jpeg)"
如果文件没有扩展名,我们可以查找文件mime类型
find . -type f -exec file -i {} + | awk -F': +' '{ if ($2 ~ /audio|video|matroska|mpeg/) print $1 }'
where(audio | video | matroska | mpeg)是mime类型正则表达式
如果你想删除它们:
find . -type f -exec file -i {} + | awk -F': +' '{ if ($2 ~ /audio|video|matroska|mpeg/) print $1 }' | while read f ; do rm "$f" done
或删除除了这些扩展之外的所有内容
find . -type f -exec file -i {} + | awk -F': +' '{ if ($2 !~ /audio|video|matroska|mpeg/) print $1 }' | while read f ; do rm "$f" done
注意!〜而不是〜
find -E /path/to -regex ".*\.(jpg|gif|png|jpeg)" > log
-E 使您免于逃离正则表达式中的parens和pipe .
-E
9 回答
这使用
file
命令尝试识别文件类型,无论文件名(或扩展名)如何 .如果
/path/to
或文件名包含字符串image
,则上述内容可能会返回虚假命中 . 在那种情况下,我建议将工作 . 可能会有更优雅的方式 .
作为@Dennis Williamson上面的回复的补充,如果你想让相同的正则表达式对文件扩展名不区分大小写,请使用-iregex:
在Mac OS上使用
如果文件没有扩展名,我们可以查找文件mime类型
where(audio | video | matroska | mpeg)是mime类型正则表达式
如果你想删除它们:
或删除除了这些扩展之外的所有内容
注意!〜而不是〜
find -E /path/to -regex ".*\.(jpg|gif|png|jpeg)" > log
-E
使您免于逃离正则表达式中的parens和pipe .