Terry
Terry

Reputation: 19

How do I remove all but the last 12 characters from filenames of different lengths using Mac terminal?

How do I remove the last 12 characters from these files, and change it from this:

RE0001_cdea_3000_001_000_0000.MP4

175c_3000_000_000_0000.MP4

To this:

001_000_0000.MP4

000_000_0000.MP4

I'm guessing is the inverse of something like for f in *; do mv "$f" "${f:12}"; done. But that removed the first 12 characters, and since the file length will change, I'm hoping the start at the file type and count the left. Thanks for your help!

Upvotes: 1

Views: 74

Answers (1)

Barmar
Barmar

Reputation: 780929

Using a negative offset counts from the end.

mv "$f" "${f: -12}"

Note that when using a negative offset you have to put a space before the offset. Otherwise it's interpreted as ${f:-defaultvalue}

But it seems like you forgot to count the .MP4 suffix, you want the last 16 characters.

mv "$f" "${f: -16}"

Upvotes: 3

Related Questions