这个庞大的存档中有许多种类的图形图像,如.jpg、.gif、.png等,我不知道所有的类型。有没有一种方法能够使用“查找”列表出所有图形图像,而不考虑它们的扩展名呢?谢谢!
这个庞大的存档中有许多种类的图形图像,如.jpg、.gif、.png等,我不知道所有的类型。有没有一种方法能够使用“查找”列表出所有图形图像,而不考虑它们的扩展名呢?谢谢!
find . -name '*' -exec file {} \; | grep -o -P '^.+: \w+ image'
示例输出:
./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
\w+
部分将排除类似 foo.svg: SVG 可缩放矢量图形
的结果。要查找例如 .svg
文件,请将 \w+
更改为 .*
。 - Løitengrep
没有-P
选项,并且file
命令的输出非常冗长。顺便说一下,如果您能解释一下您的命令,那就更好了。 - DawnSong以下更适合我,因为在我的情况下,我想将这个文件列表传输到另一个程序中。
find . -type f -exec file --mime-type {} \+ | awk -F: '{if ($2 ~/image\//) print $1}'
find . -type f -exec file --mime-type {} \+ | awk -F: '{if ($2 ~/image\//) printf("%s%c", $1, 0)}' | tar -cvf /tmp/file.tar --null -T -
第一行代码利用了以下命令:find、file和awk。第二行代码加入了tar。最好的方法是查阅本地手册,了解您特定系统的行为。
tar
或 zip
中呢? - khaverim:
的文件名,请考虑以下三种解决方案:find . -type f -exec file --mime-type {} \+ | rev | awk -F: '{ st = index($0, ":"); if (substr($0, 1, st) ~/\/egami/) print substr($0, st+1) }' | rev
find . -type f -exec file --mime-type {} \+ | awk -F: -vOFS=: '{if ($NF ~/image\//) {$NF=""; print $0} }' | sed 's/:$//'
.find . -type f -exec file --mime-type {} \+ | awk '{ n=split($0, Arr, ":"); for (i=0; i<n; i++) { if ($NF ~/image\//) { if(i == n-1) { print Arr[i] } else if(i == 0) { printf "%s", Arr[i]} else { printf "%s", Arr[i] ":" } } } }'
- 林果皞find . -type f -exec file {} \; | grep -o -P '^.+: \w+ image'
应该变得更好。
file
命令已更改,因此在 macOS Mojave 上无法正常工作。 - DawnSong-type f
,它会获取文件而不是文件夹,并执行 file
命令,因此可能比 j.holetzeck 的答案快一些。 - liginity^.+: \w+ image
,例如 hello: fake image
,则此命令存在问题。考虑使用以下解决方案:find . -type f -print0 | xargs -0 bash -c 'for arg do file -b --mime-type "$arg" | grep -q '^image/' && echo "$arg"; done' _
。解释:1. file -b
仅输出文件类型以避免文件名匹配 ^.+: \w+ image
。2. -print0 | xargs -0
能够处理文件名中的换行符,而我的其他注释无法处理这种奇特情况。3. for
循环带有尾随 _
,以覆盖文件名中的 '
或 $var
。4. --mime-type
避免了 .psd
("I"mage)和 .eps
(没有 "image")文件类型问题。 - 林果皞仅使用grep或awk查找“image”是不够的。PSD文件将通过大写字母“I”的“Image”进行识别,因此我们需要改进正则表达式,使其不区分大小写或包括大写字母I。EPS文件根本不包含单词“image”,因此我们还需要匹配“EPS”或“Postscript”,具体取决于您想要的内容。因此,这是我的改进版本:
find . -type f -exec file {} \; | awk -F: '{ if ($2 ~/[Ii]mage|EPS/) print $1}'
这是一个经过改进的版本,具有以下更改:
xargs
。:
。find . -type f |
file --mime-type -f - |
grep -F image/ |
rev | cut -d : -f 2- | rev
以下是比所选答案更高效的解决方案:
find . -type f -print0 |
xargs -0 file --mime-type |
grep -F 'image/' |
cut -d ':' -f 1
-type f
代替-name '*'
,因为前者只会搜索文件,而后者搜索文件和目录。xargs
尽可能地执行file
的参数,这比find -exec file {} \;
执行对于每个找到的文件都执行file
要快得多。grep -F
更快,因为我们只想匹配固定的字符串。cut
比awk
更快(我记得是超过5倍)。cut -d ':' -f 1
改为 rev | cut -d ':' -f 2- | rev
,以打印包含 :
的完整文件名。例如 hello : world.jpg
。 - 林果皞与同一问题相关,我刚刚发布了一个名为photofind(https://github.com/trimap/photofind)的工具。它的行为类似于普通的find命令,但专门用于图像文件,并支持基于图像文件中存储的EXIF信息过滤结果。请参阅链接的github存储库以获取更多详细信息。
find ~/Pictures \( -iname "*.jpg" -or -iname "*.jpeg" -or -iname "*.png" -or -iname "*.tif" -or -iname "*.bmp" -or -iname "*.gif" -or -iname"*.xpm" -or -iname "*.nef" -or -iname "*.cr2" -or -iname "*.arw" \) -size +20k
)。 - f3xy