Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Last active December 15, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joyrexus/5271704 to your computer and use it in GitHub Desktop.
Save joyrexus/5271704 to your computer and use it in GitHub Desktop.
Rename files w/ regexes.

Rename

Rename files with regular expressions. A filename fixer utility courtesy of Larry Wall.

Usage

rename expr [files]

Examples

rename \'s/\.orig\$//\'  *.orig
rename \'tr/A-Z/a-z/ unless /^Make/\'  *
rename \'\$_ .= \".bad\"\'  *.f
rename \'print \"\$_: \"; s/foo/bar/ if <STDIN> =~ /^y/i\'  *
find /tmp -name \'*~\' -print | rename \'s/^(.+)~\$/.#\$1/\'
#!/usr/bin/perl -w
# rename - filename fixer utility courtesy of Larry Wall
$examples = "
EXAMPLES:
rename \'s/\.orig\$//\' *.orig
rename \'tr/A-Z/a-z/ unless /^Make/\' *
rename \'\$_ .= \".bad\"\' *.f
rename \'print \"\$_: \"; s/foo/bar/ if <STDIN> =~ /^y/i\' *
find /tmp -name \'*~\' -print | rename \'s/^(.+)~\$/.#\$1/\'
";
$op = shift or die "USAGE: rename expr [files]\n" . $examples;
chomp(@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
$was = $_;
eval $op;
die $@ if $@;
rename($was, $_) unless $was eq $_;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment