Alternatives for directory listing in shell scripts

Alternatives for directory listing in shell scripts

I often have to repeat a process for every file of a certain type in a given directory.

I have been use to using this ls dialogue:

#!/bin/bash
for file in `ls /data/video`;do
  ffmbc - i $file -vcodec prores $file.mov
done;

This has some drawbacks as it tends to print the entire path to the file so you are restricted to where you put the output files.

I have recently spotted a topic discussing this and their solution gives you complete control over the destinations of your files as it picks out only the file name from the input string using the find command.

#!/bin/bash
for file in `find /data/videos/ -type f -name *.MP4 -printf "%f\n"`;do
ffmbc - i /data/videos/$file -vcodec prores /data/videos/prores/$file.mov
done;

You can tidy up the file names afterwards to remove .MP4 from the .mov filename.

find -name "*" -type f | rename 's/MP4.//g'

I would like to work out how to combine these into a single operation at some point.

Leave a Reply

Your email address will not be published. Required fields are marked *

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.