Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a bunch of files as follows:

04602635_b0294.DAT20120807164534
04602637_b0297.DAT20120807164713
04602638_b0296.DAT20120807164637
04602639_b0299.DAT20120807164819
04602640_b0298.DAT20120807164748
04602641_b0300.DAT20120807164849
04602650_b0301.DAT20120807164921
04602652_b0302.DAT20120807164956

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

b0294.DAT20120807164534
b0297.DAT20120807164713
b0296.DAT20120807164637
b0299.DAT20120807164819
b0298.DAT20120807164748
b0300.DAT20120807164849
b0301.DAT20120807164921
b0302.DAT20120807164956

EDIT

I forgot to add that I am using Solaris.

share|improve this question
add comment

2 Answers 2

up vote 11 down vote accepted
for file in * ; do
    echo mv -v "$file" "${file#*_}"
done

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

"${file#*_}"

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.

share|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
add comment

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.

share|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 Aug 10 '12 at 22:34
1  
...unless he manages to build it from source. –  sendmoreinfo May 17 '13 at 5:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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