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.
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:
^ = start of filenameOLDPREFIX = what you’re removingNEWPREFIX = what you’re adding📌 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 --versionRemove a prefix:
for f in OLDPREFIX*; do mv "$f" "${f#OLDPREFIX}"; doneReplace prefix with a new one:
for f in OLDPREFIX*; do mv "$f" "NEWPREFIX${f#OLDPREFIX}"; doneThis uses shell parameter expansion:
${f#OLDPREFIX} = remove OLDPREFIX from the start of the
filename onlyThis is 100% safe, predictable, and distro-agnostic.
See what will happen before renaming:
for f in OLDPREFIX*; do echo mv "$f" "${f#OLDPREFIX}"; doneIf it prints what you expect → remove echo and run it
for real.
Use this and you’re done:
for f in OLDPREFIX*; do mv "$f" "${f#OLDPREFIX}"; doneIf you want, you can paste:
I’ll give you the exact zero-risk command outright.