Personal tools
You are here: Home / cmgui / Wiki / RenamingFiles
Navigation
Log in


Forgot your password?
 

RenamingFiles

This is a script for renaming files with a regexp. I found this on the 'net. I think the original source was the Perl Cookbook (or something like that). It is trivial to change it so that it copies instead of renaming.<br>

<a href="rename">rename</a><br> <pre><br><img src="rename">#!/usr/bin/perl<br><br>$op = shift or die "Usage: rename expr filesn";<br><br>chomp(@ARGV = <STDIN>) unless @ARGV;<br><br>for (@ARGV) {<br> $was = $_;<br> eval $op;<br> die $@ if $@;<br> rename($was, $_) unless $was eq $_;<br>}<br><br># The first argument to the script should be a Perl expression that will be<br># evaluated for every file named on the command line, or (if there are none)<br># every file on the standard input stream, one per line. When the expression<br># is evaluated, $_ (the default variable) will contain a file name. If<br># evaluating the expression changes $_, the file will be renamed to the new<br># value of $_.<br>#<br># Examples:<br>#<br># Change spaces to underscores:<br># rename 's/ /_/g'<br># or<br># rename 'tr/ /_/'<br>#<br># Change .foo extensions to .bar:<br># rename 's/.foo$/.bar/' *<br>#<br># Change .foo to .bar in an entire directory tree:<br># find . -type f | rename 's/.foo$/.bar/'<br>#<br># Append a .bak extension to all .cc files:<br># rename '$_ .= ".bak" if /.cc$/' *<br></STDIN></pre>