Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using mp4box to move the MOOV atoms on MP4 files.

The thing is, I will have 6 files in a given folder and I don't want to run the inter 500 command six separate times.

Is it possible to do something like mp4box -inter 500 * in a directory to have the command run on all the mp4 files in that folder?

share|improve this question
up vote 5 down vote accepted

You can use the -exec option of find. Here, '{}' is replaced with the file name of every MP4 file. This will deal with all kinds of file names, even those containing spaces or newlines. You need to supply -maxdepth 1 to only search the current directory.

find . -iname "*.mp4" -maxdepth 1 -exec mp4box -inter 500 '{}' \;

An alternative, more convoluted way would involve piping the output from find into a loop with read. Here, every file is delimited by the NUL character, and you need to tell read to split the input on this character, which is achieved by -d ''. You also need to quote the variable "$file", so spaces or globbing characters in the name are retained.

find . -iname "*.mp4" -maxdepth 1 -print0 | while IFS= read -d '' -r file; do mp4box -inter 500 "$file"; done
share|improve this answer
after running the above, i get this: ce_002.mp4: unknown primary or operator am i doing something wrong here? – usr122212 Jan 22 '13 at 17:17
Well, the only thing I can think of is that your file names contain weird characters... Try the updated answer. – terdon Jan 22 '13 at 17:23
thanks for your updated answer! works great. I should read more up on exec :). – usr122212 Jan 22 '13 at 17:26
I made a few corrections. The globbing pattern in -name should always be quoted. Otherwise the * glob will be expanded before find even sees it. This means the command would fail if you were searching in another directory than the current one, or going deeper than one level. Also, the -exec option deals with any kind of filename. Using xargs is not really necessary—the while loop works better, but here the output should be null-delimited. – slhck Jan 22 '13 at 17:55
thanks for your edit and details, this works flawlessly! – usr122212 Jan 22 '13 at 20:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .