How can I batch rename files using the Terminal?

MacosShellTerminalFile RenameBatch Rename

Macos Problem Overview


I have a set of files, all of them nnn.MP4.mov. How could I rename them so that it is just nnn.mov?

Macos Solutions


Solution 1 - Macos

First, do a dry run (will not actually rename any files) with the following:

for file in *.mov
do
  echo mv "$file" "${file/MP4./}"
done

If it all looks fine, remove the echo from the third line to actually rename the files.

Solution 2 - Macos

I just successfully used Automator (first time I've bothered), and it works really well. I saved the automation as a Service. It took about 10 seconds to make something easily reusable:

  1. Open Automator.
  2. As type of document, choose "Service".
  3. Change Service receives selected "Text" to "files and folders".
  4. Consider changing "any application" to just Finder.
  5. From the sidebar, select "Files & Folders" (under Library) and from the listed actions, in the center column, drag "Rename Finder items" to the right side and drop it within "Drag actions or files here to build your workflow."
  6. Change the action you just added from "Add Date or Time" to "Make Sequential".
  7. Click "Options" at the bottom of the action and check the option "Show this action when the workflow runs".
  8. Hit "CMD+S" to save the service as something like "Replace Text"..
  9. Done!

Now you can right-click any selection in Finder, go to the Service menu and select "Replace Text", fill in how you want the text changed or replaced - and click "Continue" to apply configured changes.

Solution 3 - Macos

On mac OSX you can install the rename command via Homebrew: brew install rename

Then you can rename using find/replace from that folder on the command line:

rename 's/\.MP4\././' *

You also can see more options using man rename.

Solution 4 - Macos

To test print the operation:

for file in *.MP4.mov; do j=`echo $file | cut -d . -f 1`;j=$j".mov";echo mv \"$file\" \"$j\"; done

To make it work:

for file in *.MP4.mov; do j=`echo $file | cut -d . -f 1`;j=$j".mov";mv "$file" "$j"; done

Solution 5 - Macos

for n in *.MP4.mov
do
   mv $n $(echo $n | sed -e 's/.MP4//')
done

This will work even on really old shells that don't have parameter substitution, and it's a tad more readable to my eyes at least.

Solution 6 - Macos

OS X has a Rename Files… contextual menu item which is invoked when you select two or more files in Finder. Provides the same function as the Automator answer up above. Though Automator provides you with the tools to go further if you wish.

(I know OP asked for Terminal but 33 others like Automator response)

Solution 7 - Macos

for i in `ls *png`;do echo mv $i "${i%%.*}"@xx."${i##*.}";done

Solution 8 - Macos

vimv lets you rename multiple files using Vim's text editing capabilities.

Entering vimv opens a Vim window which lists down all files and you can do pattern matching, visual select, etc to edit the names. After you exit Vim, the files will be renamed.

[Disclaimer: I'm the author of the tool]

Solution 9 - Macos

ls -1 *.MP4.mov | while read f; do mv -i "$f" "$(basename \"$f\" .MP4.mov)"; done

Edit: completly rewritten.

Solution 10 - Macos

for i in *; 
do j=`echo $i | cut -d . -f 1`; 
j=$j".mov";
mv $i $j; 
done

this will cut everything before the first dot and appends .mov

but if some files are e.g. hi.2.mov and hi.1.mov one will be overwritten, so use it carefully ^^

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJake WildeView Question on Stackoverflow
Solution 1 - MacoskurumiView Answer on Stackoverflow
Solution 2 - MacosJohanView Answer on Stackoverflow
Solution 3 - MacosYPCrumbleView Answer on Stackoverflow
Solution 4 - MacosSONIC3DView Answer on Stackoverflow
Solution 5 - MacosvielmettiView Answer on Stackoverflow
Solution 6 - Macoswide_eyed_pupilView Answer on Stackoverflow
Solution 7 - MacosYosi TaguriView Answer on Stackoverflow
Solution 8 - MacosthameeraView Answer on Stackoverflow
Solution 9 - MacosPhilipp T.View Answer on Stackoverflow
Solution 10 - MacossharpnerView Answer on Stackoverflow