for loop in bash go though files with two specific extensions

前端 未结 1 418
轮回少年
轮回少年 2020-12-19 15:59

I want the following loop to go through m4a files AND webm files. At the moment I use two diffrent loops, the other one just replaces all the m4a from this loop. Also the fi

相关标签:
1条回答
  • 2020-12-19 16:29

    Try the following:

    for i in *.m4a *.webm; do
      echo "Converting file $converted / $numfiles : $i"
      ffmpeg -hide_banner -loglevel fatal -i "$i" "./mp3/${i%.*}.mp3"
      mv "$i" ./done
      converted=$((converted + 1))
    done
    
    • You can use for with multiple patterns (globs), as demonstrated here: *.m4a *.webm will expand to a single list of tokens that for iterates over.

      • You may want to add shopt -s nullglob before the loop so that it isn't entered with the unexpanded patterns in the event that there are no matching files.
    • ${i%.*}.mp3 uses parameter expansion - specifically, % for shortest-suffix removal - to strip any existing extension from the filename, and then appends .mp3.

    Note that the techniques above use patterns, not regular expressions. While distantly related, there are fundamental differences; patterns are simpler, but much more limited; see pattern matching.

    P.S.: You can simplify converted=$((converted + 1)) to (( ++converted )).

    0 讨论(0)
提交回复
热议问题