Bleakley
Bleakley

Reputation: 733

for loop work at multiple directory depth

I have the following for loop in bash:

for file in "$1"/*PM.mov ; do 
    ffmpeg -i "$file" -an -f framemd5 "${file}.framemd5.txt"
done

I want to adjust it so that it will run on any *PM.mov file in a given directory regardless of the directory depth of that file. Right now the loop only runs on the top level of the directory. How do I change that?

Upvotes: 0

Views: 77

Answers (2)

gniourf_gniourf
gniourf_gniourf

Reputation: 46813

Two possibilities:

  • Use globstar (and nullglob while we're at it):

    shopt -s globstar nullglob
    for file in "$1"/**/*PM.mov ; do 
        ffmpeg -i "$file" -an -f framemd5 "${file}.framemd5.txt"
    done
    
  • Use find properly (but the previous one is better, as it's only a minor change to your code):

    find "$1" -type f -name "*PM.mov" -exec sh -c 'file=$1; ffmpeg -i "$file" -an -f framemd5 "$file.framemd5.txt"' sh {} \;
    

Upvotes: 1

sigmalha
sigmalha

Reputation: 733

use the find cmd

for file in $(find $1 -name "*PM.mov") ; do 
    ffmpeg -i "$file" -an -f framemd5 "${file}.framemd5.txt"
done

Upvotes: 0

Related Questions