Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have files whose names look like this:

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

But why does this unix command not work?

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

migrated from Mar 7 '11 at 4:59

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

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 – jamessan Mar 6 '11 at 14:14
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

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

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

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

share|improve this answer
+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
ls -1 | while read item
  mv $item $(echo $item|sed -e s/.indel./.snp./g);
share|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

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";
share|improve this answer

Your Answer


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.