How to change file extensions command line MacOS

3 Comments

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)

#!/bin/sh
for f in *.$1
do
  mv $f ${f%$1}$2
done

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:

> chgext.sh png jpg

And voila! All my pngs are now jpgs.

Thanks again Ben for this great tip.

Advertisement

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: