Skip to content
Advertisement

BASH: Filter list of files by return value of another command

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

JavaScript

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.

JavaScript

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.

JavaScript

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

JavaScript

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.

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

JavaScript

Which works.

Advertisement

Answer

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):

JavaScript

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:

JavaScript

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

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement