Как я могу выполнить команду для каждого файла в данном каталоге?

1360
usr122212

Я использую mp4boxдля перемещения атомов MOOV на файлах MP4.

Дело в том, что у меня будет 6 файлов в данной папке, и я не хочу запускать inter 500команду шесть раз.

Можно ли сделать что-то вроде mp4box -inter 500 *в каталоге, чтобы команда запускалась для всех файлов mp4 в этой папке?

0

1 ответ на вопрос

5
terdon

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 
после запуска выше, я получаю это: `ce_002.mp4: неизвестный основной или оператор` я делаю что-то здесь не так? usr122212 11 лет назад 0
Ну, единственное, о чем я могу думать, это то, что имена ваших файлов содержат странные символы ... Попробуйте обновленный ответ. terdon 11 лет назад 0
спасибо за ваш обновленный ответ! работает отлично. Я должен читать больше на exec :). usr122212 11 лет назад 0
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 11 лет назад 2
спасибо за ваше редактирование и детали, это работает безупречно! usr122212 11 лет назад 0

Похожие вопросы