2025-12-10 16:29:03 EST

M: I've got a bunch of files I want to rename, all of which start with the same prefix. Is there a quick Linux command line function? I feel like there is. I can't remember what.

G: Yes—this is a classic one-liner job. The tool you’re thinking of is usually rename, and the shell builtin for + parameter expansion also works everywhere.

Below are the two fastest, safest patterns.


✅ Option 1: rename (cleanest, if installed)

Remove a prefix from all files:

rename 's/^OLDPREFIX//' *

Replace one prefix with another:

rename 's/^OLDPREFIX/NEWPREFIX/' *

This uses regex:

📌 Works on most modern Linux distros, but there are two incompatible versions of rename (Perl vs util-linux). The above form is the Perl version.

To check which one you have:

rename --version

✅ Option 2: Portable Bash (works everywhere, no dependencies)

Remove a prefix:

for f in OLDPREFIX*; do mv "$f" "${f#OLDPREFIX}"; done

Replace prefix with a new one:

for f in OLDPREFIX*; do mv "$f" "NEWPREFIX${f#OLDPREFIX}"; done

This uses shell parameter expansion:

This is 100% safe, predictable, and distro-agnostic.


See what will happen before renaming:

for f in OLDPREFIX*; do echo mv "$f" "${f#OLDPREFIX}"; done

If it prints what you expect → remove echo and run it for real.


✅ TL;DR

Use this and you’re done:

for f in OLDPREFIX*; do mv "$f" "${f#OLDPREFIX}"; done

If you want, you can paste:

I’ll give you the exact zero-risk command outright.