Header image

It's full of stars

Where documentation meets reality


Remove last n characters of file in MacOs

By Tobias Hofmann November 30, 2018 Posted in SAP

Reading time: 1 min read


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/.

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

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