Remove last n characters of file in MacOs

With MacOS and finder you can easily substitute characters of files using the rename functionality. Just select 2 or more files, right click, and inform the character you want to substitute, like _ with space.

To remove the last N characters from a file that looks like Text-2018221112.mp4 to Text is more complicated. The rename dialog does not understand regex. What you can use is the shell and rename

Install rename

brew install rename

Go to the directory with the files and run

rename -n 's/.{11}.mp4/\.mp4/' *

Rename uses the well known sed syntax s/char/replace/.

  • -n runs the replace in simulation mode. It will print the result, without renaming the files yet. Perfect for testing.
  • {11} number of characters to replace
  • \.mp4 is to insert mp4 again, as .{}11.mp4 will replace also the file suffix

In case the output matches your goal, run rename without -n and the files will be renamed.

rename 's/.{11}.mp4/\.mp4/' *