77

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

2
  • 1
    Install muCommander. It has the Total Commander style batch renamer. Why to waste time on that crap 100 chars of mindblowing batch command?
    – Nakilon
    Commented Aug 17, 2013 at 22:34
  • @Nakilon because learning the command line has bigger return on investment
    – Julien__
    Commented Apr 25, 2017 at 13:14

10 Answers 10

104

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.

5
  • 19
    Could not figure out why this was not working... there was output and it looked like it was, but it was not actually doing anything. Well I finally figured out that you need to remove the ECHO statement to actually get it to do something (on OSX 10.7 anyways). So if you keep the echo statement then you can test it to see if the renaming is going to work like you want, then remove the echo to get it to work. So equivalent from Windows of ren *-hd.png *.png is: for file in *.png do mv "$file" "${file/-hd/}" done
    – jsherk
    Commented Jul 12, 2012 at 1:10
  • 4
    to run the script you need to: 1. save it in rename.sh file; 2. make it executable chmod 755 rename.sh; 3. launch it from the current folder where you have files to rename and shell script ./rename.sh; 4. if result is good remove echo from the script and launch it again. I needed to add extension to group of files. so my action in the scrip was like this: mv "$file" "${file}.jpeg" Commented Sep 10, 2016 at 8:17
  • This is a script and needs to be created as a file and then run right? Clues? Commented May 21, 2018 at 6:10
  • Running as single line cmd like this: for file in *.mov; do echo mv "$file" "${file/MP4./}"; done, the ; does the trick.
    – Evi Song
    Commented Apr 29, 2021 at 14:58
  • 4
    For other use cases, note the general format of the command: echo mv "$file" "${file/[replace_this]/[with_this]}"
    – HLeb
    Commented Jul 20, 2021 at 15:08
35

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.

6
  • I downvoted it because the question has "terminal" tag.
    – v42
    Commented Dec 13, 2013 at 17:11
  • Great answer, even if it is not what the OP asked for. Any excuse for me to learn Automator is welcomed :-) BTW, it does not work on a folder, but it does work on multiply selected files.
    – phatmann
    Commented Aug 12, 2014 at 22:43
  • Fantastic! Don't care about the OP! Commented Aug 14, 2015 at 3:46
  • Don't you mean change from "Add Time and Date" to "Replace Text" ? Commented May 21, 2018 at 6:08
  • What wildcards are accepted in the find and replace fields? Is it Grep compatible? Commented May 21, 2018 at 6:09
15

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.

14

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
1
  • Thanks for the one line solution
    – semihcosu
    Commented Sep 12, 2017 at 8:38
11
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.

1
  • I have applied this to files that use _MG_ in their name that I needed to change to -IMG_ perfectly by modifying with a * and a * in the middle. Worked a charm. Thank you! Commented Oct 1, 2014 at 1:53
5

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)

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

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]

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

Edit: completly rewritten.

3
  • If it does what you want, remove the -i
    – Philipp T.
    Commented Mar 22, 2011 at 16:05
  • i mean, it thinks there should be a continuation of the line
    – Jake Wilde
    Commented Mar 22, 2011 at 16:07
  • what it did was rename it to "nnn.MP4.mov".mov
    – Jake Wilde
    Commented Mar 22, 2011 at 16:07
0
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 ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.