Bulk rename, change prefix

I'd say the simplest it to just use the rename command which is common on many Linux distributions. There are two common versions of this command so check its man page to find which one you have:

## rename from Perl (common in Debian systems -- Ubuntu, Mint, ...)
rename 's/^TestSR/CL/' *

## rename from util-linux-ng (common in Fedora systems, Red Hat, CentOS, ...)
rename TestSR CL *

If you want to use the version from util-linux-ng in a Debian system, it is available under the name rename.ul


for name in TestSR*
do
    newname=CL"$(echo "$name" | cut -c7-)"
    mv "$name" "$newname"
done

This uses bash command substitution to remove the first 6 characters from the input filename via cut, prepends CL to the result, and stores that in $newname. Then it renames the old name to the new name. This is performed on every file.

cut -c7- specifies that only characters after index 7 should be returned from the input. 7- is a range starting at index 7 with no end; that is, until the end of the line.

Previously, I had used cut -b7-, but -c should be used instead to handle character encodings that could have multiple bytes per character, like UTF-8.


Shell parameter expansion is enough for simple transformations like this. Command substitution is less efficient because of the need to spawn extra processes (for the command substitution itself and the cut/sed).

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