I have a bunch of files as follows:


I need to rename them to exclude the prefix. It needs to look like this..



I forgot to add that I am using Solaris.

for file in * ; do
    echo mv -v "$file" "${file#*_}"

run this to satisfy that everything is ok.
if it is, remove echo from command and it will rename files as you want.


is a usual substitution feature in the shell. It removes all chars before the first _ symbol (including the symbol itself). For more details look here.

| improve this answer | |
  • Could you please explain what exactly is happening in "${file#*_}"? – Pieter van Niekerk Aug 10 '12 at 13:23
  • I added explanation to my answer. – rush Aug 10 '12 at 13:31
  • IMO, most all scripts should be written to echo the desired command output. But don't then remove the echo, just pipe the script output into bash. – Jim L. May 29 '19 at 16:19

You can use the tool rename for this:

rename "s/.*_//" *

If you append -n it won't rename anything and just show you what would have been done without -n.

In response to rush's comment: my rename is is actually a link to prename shipped with Debian's and Ubuntu's perl package.

| improve this answer | |
  • 5
    Note, that rename not always is the same on different systems and sometimes its syntax may differ. – rush Aug 10 '12 at 13:34
  • 1
    The asker has now added that he's using Solaris. So this rename command (which is specific to Debian and derivatives) is not available to him. – Gilles 'SO- stop being evil' Aug 10 '12 at 22:34
  • 1
    ...unless he manages to build it from source. – sendmoreinfo May 17 '13 at 5:47
  • @Gilles perl-rename is not at all specific to Debian. It's just not called rename but perl-rename or prename in other distributions. It should always be in the repos though and one is installed with perl as well. – terdon Apr 27 '17 at 11:06
  • @terdon A command to rename files whose basic usage is a perl expression is not specific to Debian. Such a command, called rename and likely to be installed without explicitly requesting a package which is not called just rename, is specific to Debian. Other Linux distributions call the command prename or perl-rename to avoid confusion with the util-linux rename and don't bundle it with the perl package, and I'm not aware of any non-Linux unix that ships it under the name rename or that ships it in a default installation under any name. – Gilles 'SO- stop being evil' Apr 27 '17 at 11:18

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.