I have a directory with more than a 1,000 folders on my Mac. Each folder is a person's last name and then first name separated by a comma. For example - Smith, John.

I want to rename all the directories with a shell script so Smith, John becomes John Smith

These directories will be ingested into a DAM system which will take folder names and make all the items under the directory into keywords. The way they are now Smith, John will tag each file under it with both "smith" and "John" as separate keywords. If I can rename all the files to "firstname lastname" without a comma all the files under it can be auto tagged with the person's name as a keyword in the DAM.

Thanks for any ideas!

3 Answers 3


If you have the rename written by Larry Wall, then you can use it. It is the sed for filenames. Be careful, some systems have a different tool with the name rename.

rename -n -e 's/([^,]+), ([^,]+)/$2 $1/' «list_of_files»

Replace «list_of_files», e.g. with *. Once tested, remove the -n.


Perl version of rename utility could do it:

rename 's/(.+), (.+)/$2 $1/' *

NOTE: make a backup of the directory tree before running this command. It should work but you didn't give too many details about the names of your directories.

$ find .  -type d -maxdepth 1 -exec basename "{}" \; | grep -v '^.$' | awk -F", " '{ print "mv \""$0"\" \"" $2, $1"\""  }' | tee rename.sh
mv "Doe, Jon" "Jon Doe"
mv "Smith, Bob" "Bob Smith"
mv "Roe, Jane" "Jane Roe"

You can then chmod u+x rename.sh and run it after giving the output from tee a once-over.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .