Skip to main content
4 of 4
please for whatever you love most, don't make an abstract example that doesn't actually reflect your directory structure. How can we possibly give accurate answers in that case? If I have messed it up, please correct, but please make it correct. I had to use your answer to understand!
Zanna
  • 70.9k
  • 59
  • 222
  • 330

How to access Folder/Subfolders/Files using nested loops for file conversions?

In a structure like this:

/home/me/Videos/Downloads/Videos/xml tutorials/Main/video1.mp4
/home/me/Videos/Downloads/Videos/xml tutorials/Main/video2.mp4
....
/home/me/Videos/Downloads/Videos/java tutorials/Main/video1.mp4
/home/me/Videos/Downloads/Videos/java tutorials/Main/video2.mp4
....

I need to convert the videos and save the new files like this:

/home/me/Videos/Downloads/Videos/xml tutorials/Converted/video1.mp4
/home/me/Videos/Downloads/Videos/xml tutorials/Converted/video2.mp4
/home/me/Videos/Downloads/Videos/java tutorials/Converted/video1.mp4
/home/me/Videos/Downloads/Videos/java tutorials/Converted/video2.mp4

I want to convert these videos using ffmpeg by running a nested loop through the Folder/Subfolders/Videos.

By running this I can loop through all files in the current directory:

for f in *; do ffmpeg -i "$f" ... ; done

How can I use nested loops for this, so that bash itself can automatically browse Videos/xml tutorials/main/ and do the conversion, put them in Videos/xml tutorials/converted, and itself move to Videos/java tutorials/main/?