Created
July 29, 2019 00:10
-
-
Save stephenturner/7bda82650a3ebceebb76fd4247d1b170 to your computer and use it in GitHub Desktop.
Perl rename script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use warnings; | |
use strict; | |
use Getopt::Long; | |
Getopt::Long::Configure('bundling'); | |
my ($verbose, $no_act, $force, $op); | |
die "Usage: rename [-v] [-n] [-f] perlexpr [filenames]\n" | |
unless GetOptions( | |
'v|verbose' => \$verbose, | |
'n|no-act' => \$no_act, | |
'f|force' => \$force, | |
) and $op = shift; | |
$verbose++ if $no_act; | |
if (!@ARGV) { | |
print "reading filenames from STDIN\n" if $verbose; | |
@ARGV = <STDIN>; | |
chop(@ARGV); | |
} | |
for (@ARGV) { | |
my $was = $_; | |
eval $op; | |
die $@ if $@; | |
next if $was eq $_; # ignore quietly | |
if (-e $_ and !$force) | |
{ | |
warn "$was not renamed: $_ already exists\n"; | |
} | |
elsif ($no_act or rename $was, $_) | |
{ | |
print "$was renamed as $_\n" if $verbose; | |
} | |
else | |
{ | |
warn "Can't rename $was $_: $!\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment