Linux 使用 find? 列出所有图形图像文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16758105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:02:56  来源:igfitidea点击:

List all graphic image files with find?

linuxgraphicsfind

提问by Edward

There are many types of graphic images in this huge archive such as .jpg, .gif, .png, etc. I don't know all the types. Is there a way with 'find' to be able to have it list all the graphic images regardless of their dot extension name? Thanks!

这个庞大的档案中有许多类型的图形图像,例如.jpg、.gif、.png 等,我不知道所有类型。有没有办法通过“查找”来列出所有图形图像,而不管它们的点扩展名如何?谢谢!

采纳答案by j.holetzeck

This should do the trick

这应该可以解决问题

find . -name '*' -exec file {} \; | grep -o -P '^.+: \w+ image'

example output:

示例输出:

./navigation/doc/Sph?rische_Trigonometrie-Dateien/bfc9bd9372f650fd158992cf5948debe.png: PNG image
./navigation/doc/Sph?rische_Trigonometrie-Dateien/6564ce3c5b95ded313b84fa918b32776.png: PNG image
./navigation/doc/subr_1.jpe: JPEG image
./navigation/doc/Astroanalytisch-Dateien/Gamma.gif: GIF image
./navigation/doc/Astroanalytisch-Dateien/deltaS.jpg: JPEG image
./navigation/doc/Astroanalytisch-Dateien/GammaBau.jpg: JPEG image

回答by Nicolas Appriou

find . -type f -exec file {} \; | grep -o -P '^.+: \w+ image'

should even be better.

甚至应该更好。

回答by f3xy

The following suits me better since in my case I wanted to pipe this list of files to another program.

以下更适合我,因为在我的情况下,我想将此文件列表通过管道传输到另一个程序。

find . -type f -exec file --mime-type {} \+ | awk -F: '{if ( ~/image\//) print }'

If you wanted to tar the images up (as someone in the comments) asked

如果您想将图像压缩(如评论中的某个人)所问

find . -type f -exec file --mime-type {} \+ | awk -F: '{if ( ~/image\//) printf("%s%c", , 0)}' | tar -cvf /tmp/file.tar --null -T -

回答by fltman

Grepping or using awk for "image" only will not do it. PSD-files will be identified by "Image" with a capital "I" so we need to improve the regexp to either be case insensitive or also include the capital I. EPS-files will not contain the word "image" at all so we need to also match for "EPS" or "Postscript" depending on what you want. So here is my improved version:

Grepping 或仅将 awk 用于“图像”是行不通的。PSD 文件将由带有大写字母“I”的“Image”标识,因此我们需要改进正则表达式以不区分大小写或还包括大写字母 I。EPS 文件根本不包含“图像”一词,因此我们还需要根据您的需要匹配“EPS”或“Postscript”。所以这是我的改进版本:

find . -type f -exec file {} \; | awk -F: '{ if ( ~/[Ii]mage|EPS/) print }'

回答by juusor

Related to the same problem, I just published a tool called photofind(https://github.com/trimap/photofind). It behaves like the normal find-command but is specialized for image files and supports filtering of results also based on the EXIF-information stored within the image files. See the linked github-repo for more details.

与同样的问题相关,我刚刚发布了一个名为photofind的工具(https://github.com/trimap/photofind)。它的行为类似于普通的查找命令,但专门用于图像文件,并支持基于存储在图像文件中的 EXIF 信息的结果过滤。有关更多详细信息,请参阅链接的 github-repo。

回答by Jian Weihang

Below is a more performant solution compared to the chosen answer:

与所选答案相比,以下是性能更高的解决方案:

find . -type f -print0 |
  xargs -0 file --mime-type |
  grep -F 'image/' |
  cut -d ':' -f 1
  1. Use -type finstead of -name '*'since the former will search only files while the latter search both files and directories.
  2. xargsexecute filewith arguments as many as possible, which is super fast compared to find -exec file {} \;which executes filefor each found.
  3. grep -Fis faster since we only want to match fixed string.
  4. cutis faster than awk(more than 5 times faster as I can recall).
  1. 使用-type f而不是-name '*'因为前者只搜索文件而后者搜索文件和目录。
  2. xargsfile使用尽可能多的参数执行,与为每个找到的find -exec file {} \;执行相比,这是超快的file
  3. grep -F更快,因为我们只想匹配固定字符串。
  4. cutawk(我记得快 5 倍以上)更快。