I want to write a script, which automates the following procedure: Every week I copy about 5-10 video files to a directory and delete the audio stream of each file, using avcodec. I thought this was a good starting point to write my first script in bash. It should check the filenames in the directory (00001.MTS,00002.MTS,...) and execute the command for each file. I come from python and matlab, where I would be able to write this script using a for or a while loop.

I would be glad to not get the finished code for my problem, but a few links where I can find the necessary information.

Thanks, Chris


Check man find. It's a flexible and powerful tool that you should definitely think about whenever you hear "several files from a directory". Whether you want to just find them (-print), or in this case run a command on every one of them (-exec):

find "$HOME"/some/videos -type f -name "*.MTS" \
    -exec av_encoder_too --remove-audio ... {} \;


  • the literal semicolon at the end needs to be escaped with a backslash.
  • the {} is a literal pair of braces -- that is where find will substitute in each file it finds when runs the commands.
  • the first backslash at the end of the line is just to split a long command into two lines. You could just as easily omit it and write it all as one long command line

Of course you'll need to supply correct values for

  • the directory "$HOME"/some/videos
  • the encoder tool and it's args av_encoder_too --remove-audio ...

After you have this working, as a next step on your path to learning, I recommend exploring the xargs command -- It's a different approach to issues of "do something to many files".

Happy hacking,

  • thanks ./ddd! I'll try this next week, as I am only working one day a week on the linux computer. – slush puppie Mar 1 at 11:19
  • I would like to use the following command, to cut the audio stream of the video: "avconv -i [input_file] -vcodec copy -an [output_file]" What can I supply for [input file] and [output file], if I want to have it done for all the files in the directory? – slush puppie Mar 27 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.