How to change file extensions command line MacOS


Linux has a handy rename command which is great for easily renaming file extensions within a directory.

> rename .htm .html *.htm

MacOS however has no such command.

Fortunately, Ben Han documented a nice work around by creating a simple script.
(Click here for a reminder on how to create MacOS scripts)

for f in *.$1
  mv $f ${f%$1}$2

It uses the mv command and loops through your directory doing the move based on whatever parameters you pass in.

For example to change all my pngs to jpgs all I have to do now is navigate to the target directory and type:

> png jpg

And voila! All my pngs are now jpgs.

Thanks again Ben for this great tip.


How to zip all files of certain file extension (UNIX, MacOS)

Leave a comment

As part of trying to do more things from the command line here’s a handy command for zipping files by type on MacOS from the command line.

find . -name "*.jpg" -print | zip source -@

You can find this an other commands from the zip man page.

%d bloggers like this: