I have files whose names look like this:

Sim1-2_40.36.chr20_sb.foo.indel.novoalign.sam
Sim1-2_40.36.chr20_sb.foo.indel.bwa.sam

What I want to do is to replace all indel with snp in the names yielding

Sim1-2_40.36.chr20_sb.foo.snp.novoalign.sam
Sim1-2_40.36.chr20_sb.foo.snp.bwa.sam

But why this unix command doesn't work

$ rename 's/indel/snp/' *.sam
link|improve this question

50% accept rate
I can't see the documentation for rename mentioning support for commands like 's/indel/snp/' , which rename utility are you using ? – nos Mar 6 '11 at 14:07
Looks like the OP is referring to man.cx/prename – jamessan Mar 6 '11 at 14:14
2  
What doesn't work about that command? Are you getting an error? Is it not changing the filenames? Something else? – jamessan Mar 6 '11 at 14:15
feedback

migrated from stackoverflow.com Mar 7 '11 at 4:59

This question came from our site for professional and enthusiast programmers.

4 Answers

Because you’re using the wrong rename program. This is the right one.

link|improve this answer
If it isn't on CPAN (1, 2, 3, 4, 5), it might as well not exist. – daxim Mar 6 '11 at 19:49
@daxim: CPAN hates programs. – tchrist Mar 6 '11 at 20:47
You're unreasonable. – daxim Mar 7 '11 at 2:53
feedback

The "normal" rename utility (part of util-linux) takes 3 or more arguments (as mentioned in TFM..)

rename FROM TO filespec...

Thus, you are (probably) looking for:

rename .indel. .snp. *.sam

Test this on a scratch directory first, as you may be using the another implementation of rename.

Or check the man page. cough

link|improve this answer
2  
+1 for not immediately jumping to regexes. :) But you might want dots around your indel and snp, so paths like blah.stuffindelsomeotherstuff.x don't get changed as well. – cHao Mar 6 '11 at 14:30
Good point. Added. – Simon Mar 6 '11 at 14:32
feedback
ls -1 | while read item
do
  mv $item $(echo $item|sed -e s/.indel./.snp./g);
done
link|improve this answer
Why ls -1 | while read item rather than for item in *.sam? – cHao Mar 6 '11 at 14:36
@user280476: your code will do strange things if you have files with spaces in the name. – Mat Mar 6 '11 at 17:48
… or a large number of files. – JdeBP Aug 27 '11 at 13:10
feedback

rename doesn't do sed-style substitutions. This very short Perl script will let you do regmv *.sam s/indel/snp/:

#!/usr/bin/perl -w
#
# regmv - Rename files using a regular expression
#
# This program renames files by using a regular expression to
# determine their new name.
#
# Usage: regmv file [file ...] regexp
#
# $Id: regmv,v 1.1 1998/10/14 17:07:55 blrfl Exp $
#

sub at_clean()
{
    my $message = $@;
    $message =~ s|\s+at /.*$||s;
    return $message;
}

(@ARGV > 1)
    || die "Usage: regmv [options] file [file ...] regexp\n";

my $expr = pop @ARGV;

$_ = 'SomeValue';
eval "\$_ =~ $expr";
die "Yuck: " . at_clean() if $@;

foreach (@ARGV)
{
    my $old = $_;

    eval "\$_ =~ $expr";

    next if $_ eq $old;

    (rename $old, $_)
        || die "Rename failed: $!\n";
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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