50

How can I bulk replace the prefix for many files?

I have a lot of files like

  • TestSRConnectionContext.h
  • TestSRConnectionContext.m

I would like to change all them to

  • CLConnectionContext.h
  • CLConnectionContext.m

How would I do this?

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

  • Thx, i got this right now by myself too. Marking as answer. Thx – ErikTJ Sep 6 '12 at 16:35
  • This works nicely, thanks. Can you explain what the -b7- does? Is this reference to the byte-list? ss64.com/bash/cut.html – imjared Sep 26 '13 at 16:01
  • @imjared Updated the answer with more details. – mrb Sep 26 '13 at 17:08
55

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

  • 7
    A caveat: multiple versions of rename exist in the wild. Check your local rename documentation to figure out how to use yours. – jw013 Sep 6 '12 at 17:39
  • To remove the alias do rename TestSR '' * – Bsienn Sep 21 '17 at 7:38
  • hmm... rename "\<pre\>" "" * -> Unterminated <> operator at (user-supplied code). – Flash Thunder Jul 25 at 8:48
31

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
  • Explanation: TestSR* finds the files, CL${f#TestSR} will be the new name. The stuff after the # will be substituted from the filename (f). – Joe Eifert Jun 1 '18 at 11:31
4

Here's one way:

ls *.{h,m} | while read a; do n=CL$(echo $a | sed -e 's/^Test//'); mv $a $n; done
  • ls *.{h,m} --> Find all files with .h or .m extension
  • n=CL --> Add a CL prefix to the file name
  • sed -e 's/^Test//' --> Removes the Test prefix from the file name
  • mv $a $n --> Performs the rename
3

You can try with:

for i in TestSR*; do mv ${i} ${i/#TestSR/CL}; done

See man bash (section "Parameter Expansion") for details.

1

Well, it wasn't as hard as i though.

$ for f in TestSR*.m; do mv $f CL$(echo $f | cut -c7-); done;
$ for f in TestSR*.h; do mv $f CL$(echo $f | cut -c7-); done;
  • 1
    In case it helps for the future, you don't have to repeat the commands for your two patterns; you can use (for example): for f in TestSR*.[mh], for f in TestSR*.{m,h}, for f in TestSR*.m TestSR*.h. – mrb Sep 6 '12 at 22:37
1

If you need something more perlish you can do this

perl -e 'for(@ARGV) { rename($_, $n) if( ($n = $_ ) =~ s/^TestSR/CL/) }' *

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.