Personal tools
You are here: Home cmgui Wiki RenamingFiles
Views

RenamingFiles

last edited 1 year ago by pbier
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.
rename

#!/usr/bin/perl

$op = shift or die "Usage: rename expr [files]\n";

chomp(@ARGV = ) unless @ARGV;

for (@ARGV) {
$was = $_;
eval $op;
die $@ if $@;
rename($was, $_) unless $was eq $_;
}

# The first argument to the script should be a Perl expression that will be
# evaluated for every file named on the command line, or (if there are none)
# every file on the standard input stream, one per line. When the expression
# is evaluated, $_ (the default variable) will contain a file name. If
# evaluating the expression changes $_, the file will be renamed to the new
# value of $_.
#
# Examples:
#
# Change spaces to underscores:
# rename 's/ /_/g'
# or
# rename 'tr/ /_/'
#
# Change .foo extensions to .bar:
# rename 's/\.foo$/.bar/' *
#
# Change .foo to .bar in an entire directory tree:
# find . -type f | rename 's/\.foo$/.bar/'
#
# Append a .bak extension to all .cc files:
# rename '$_ .= ".bak" if /\.cc$/' *

comments:
... --pbier, Fri, 16 Feb 2007 16:15:34 +1300 reply
This script did not work for me so I grabbed the original from the perl cook book http://www.unix.org.ua/orelly/perl/cookbook/ch09_10.htm

Contributing to this site

Please add to the wiki any relevant information that you think might be useful to other users of this website. For example, you might like to contribute your experiences, questions and answers.

You are encouraged to contribute to this site regardless of your level of experience. Contributions are welcomed from new and regular visitors.

If you ask a question and receive an answer from a developer you should record it in the wiki. This information is extremely useful and can help other users overcome the same problem.

See how to add and edit pages for more information.