Views
RenamingFiles
last edited 1 year ago by pbier
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:
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