How to rename files in batch with Unix rename command?

6

1

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

neversaint

Posted 2011-03-06T14:02:59.973

Reputation: 285

I can't see the documentation for rename mentioning support for commands like 's/indel/snp/' , which rename utility are you using ? – nos – 2011-03-06T14:07:41.680

Looks like the OP is referring to http://man.cx/prename

– jamessan – 2011-03-06T14:14:01.663

2What doesn't work about that command? Are you getting an error? Is it not changing the filenames? Something else? – jamessan – 2011-03-06T14:15:04.470

Answers

5

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

tchrist

Posted 2011-03-06T14:02:59.973

Reputation: 218

1

If it isn't on CPAN (1, 2, 3, 4, 5), it might as well not exist.

– daxim – 2011-03-06T19:49:35.537

1@daxim: CPAN hates programs. – tchrist – 2011-03-06T20:47:53.280

1You're unreasonable. – daxim – 2011-03-07T02:53:42.030

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

Simon

Posted 2011-03-06T14:02:59.973

Reputation: 141

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 – 2011-03-06T14:30:18.743

Good point. Added. – Simon – 2011-03-06T14:32:41.070

2

ls -1 | while read item
do
  mv $item $(echo $item|sed -e s/.indel./.snp./g);
done

user280476

Posted 2011-03-06T14:02:59.973

Reputation:

Why ls -1 | while read item rather than for item in *.sam? – cHao – 2011-03-06T14:36:07.770

@user280476: your code will do strange things if you have files with spaces in the name. – None – 2011-03-06T17:48:53.037

… or a large number of files. – JdeBP – 2011-08-27T13:10:16.297

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

Blrfl

Posted 2011-03-06T14:02:59.973

Reputation: 519