BASH:通过另一个命令的返回值过滤文件列表 [英] BASH: Filter list of files by return value of another command

查看:81
本文介绍了BASH:通过另一个命令的返回值过滤文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列的目录,其中包含(主要是)视频文件

I have series of directories with (mostly) video files in them, say

test1
  1.mpg
  2.avi
  3.mpeg
  junk.sh
test2
  123.avi
  432.avi
  432.srt
test3
  asdf.mpg
  qwerty.mpeg

我使用目录名称(基于其他参数)创建一个变量( video_dir ),并将其与 find 结合使用以生成基本列表.然后,我根据另一个变量( video_type )过滤文件类型(因为目录中有时存在非视频文件),并通过 egrep 将其传递给文件类型.然后,我重新整理列表,然后将其保存到文件中.后来, mplayer 使用该文件在列表中进行幻灯片放映.我目前使用以下命令来完成该任务.我敢肯定这是一种可怕的方法,但是它对我有用,即使在大目录下也非常快.

I create a variable (video_dir) with the directory names (based on other parameters) and use that with find to generate the basic list. I then filter based on another variable (video_type) for file types (because there is sometimes non-video files in the dirs) piping it through egrep. Then I shuffle the list around and save it out to a file. That file is later used by mplayer to slideshow through the list. I currently use the following command to accomplish that. I'm sure it's a horrible way to do it, but it works for me and it's quite fast even on big directories.

video_dir="/test1 /test2"
video_types=".mpg$|.avi$|.mpeg$"

find ${video_dir} -type f    |
  egrep -i "${video_types}"  |
  shuf > "$TEMP_OUT"

我现在想添加基于视频文件的分辨率高度过滤掉文件的功能.我可以从那里得到.

I now would like to add the ability to filter out files based on the resolution height of the video file. I can get that from.

mediainfo --Output='Video;%Height%' filename

这只是返回一个数字.我尝试使用find的-exec功能在每个文件上运行该命令.

Which just returns a number. I have tried using the -exec functionality of find to run that command on each file.

 find ${video_dir} -type f -exec mediainfo --Output='Video;%Height%' {} \;

但这只是返回高度列表,而不是文件名,我不知道如何基于比较来拒绝那些高度,例如< 480.我可以为下一个循环做一个准备,但这似乎是个坏主意.

but that just returns the list of heights, not the filenames and I can't figure out how to reject ones based on a comparison, like <480. I could do a for next loop but that seems like a bad (slow) idea.

使用来自@ mark-setchell的信息,我将其修改为

Using info from @mark-setchell I modified it to,

video_dir="test1"

find ${video_dir} -type f   \
   -exec bash -c 'h=$(mediainfo --Output="Video;%Height%" "$1"); [[ $h -gt 480 ]]' _ {} \; -print

哪个工作.

推荐答案

您可以将 egrep 替换为以下内容,这样您仍然可以在 find 命令( -iname 不区分大小写,而 -o 表示逻辑或):

You can replace your egrep with the following so you are still inside the find command (-iname is case insensitive and -o represents a logical OR):

find test1 test2 -type f                                       \
     \( -iname "*.mpg" -o -iname "*.avi" -o -iname "*.mpeg" \) \
     NEXT_BIT

然后,NEXT_BIT可以 -exec bash 并以状态0或1退出,具体取决于您希望包含还是排除当前文件.因此它将如下所示:

The NEXT_BIT can then -exec bash and exit with status 0 or 1 depending on whether you want the current file included or excluded. So it will look like this:

-exec bash -c 'H=$(mediainfo -output ... "$1"); [ $H -lt 480 ] && exit 1; exit 0' _ {} \;

因此,在有关多余的 exit 语句的注释中注意@tripleee建议,我得到了:

So, taking note of @tripleee advice in comments about superfluous exit statements, I get this:

find test1 test2 -type f                                       \
    \( -iname "*.mpg" -o -iname "*.avi" -o -iname "*.mpeg" \)  \
    -exec bash -c 'h=$(mediainfo ...options... "$1"); [ $h -lt 480 ]' _ {} \; -print

这篇关于BASH:通过另一个命令的返回值过滤文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆