6

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 does this unix command not work?

$ rename 's/indel/snp/' *.sam
3
  • 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, 2011 at 14:07
  • Looks like the OP is referring to man.cx/prename
    – jamessan
    Mar 6, 2011 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, 2011 at 14:15

4 Answers 4

5

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

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

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

2
  • 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, 2011 at 14:30
  • Good point. Added.
    – Simon
    Mar 6, 2011 at 14:32
2
ls -1 | while read item
do
  mv $item $(echo $item|sed -e s/.indel./.snp./g);
done
3
  • Why ls -1 | while read item rather than for item in *.sam?
    – cHao
    Mar 6, 2011 at 14:36
  • @user280476: your code will do strange things if you have files with spaces in the name.
    – Mat
    Mar 6, 2011 at 17:48
  • … or a large number of files.
    – JdeBP
    Aug 27, 2011 at 13:10
0

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";
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .