Search code examples
macosbashunixfind-util

How to change filename extensions in subdirectories on Mac OS X


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.


Solution

  • 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.