4

I have been trying to iterate though a directory structure where I want to change the file extension from .mv4 to .mp4.

The problem is that there are spaces in many of the file names, and I have not been successful in iterating the directory structure.

I am doing this in the terminal.

There are examples for changing extensions in a single directory, but not for subdirectories and where the file names have spaces in them.

mklement0
  • 382,024
  • 64
  • 607
  • 775
svjim
  • 191
  • 1
  • 12

1 Answers1

5

You can use the -exec argument of "find" to do this:

 find . -type f -name "*.mv4" -exec sh -c 'mv "$1" "${1%.mv4}.mp4"' _ {} \;

Issue the "find" command from the base directory of your directory structure containing the mv4 files, or specify that directory in place of the "." right after "find" in the above line of code.

I tested this on my Mac running Yosemite. It works for me with filenames that contain spaces.

hft
  • 1,245
  • 10
  • 29
  • Thanks, I made an edit, but still just using find... just added the -iname "*.mv4" to only hit the mv4 files – hft Mar 09 '15 at 03:38
  • Thanks. I changed it. I think I like the case-sensitive solution because the original question refers to the file extensions ".mv4" and ".mp4" in lowercase. – hft Mar 09 '15 at 04:07
  • Great. Note that another advantage of using `${1%.*}` instead of `${1%.mv4}` is that it would leave you free to choose the case-matching behavior via `-name` vs. `-iname`. – mklement0 Mar 09 '15 at 04:13