Skip to content

Instantly share code, notes, and snippets.

@varvaruc
Forked from jcward/_readme.txt
Created April 3, 2020 14:11
Show Gist options
  • Save varvaruc/18dae866e437727d64e1c2bbd7e3e8f5 to your computer and use it in GitHub Desktop.
Save varvaruc/18dae866e437727d64e1c2bbd7e3e8f5 to your computer and use it in GitHub Desktop.
A Perl-regex based copy script, derived from the rename script
I really like the regex syntax of the rename perl script, e.g.:
rename "s/OldFile/NewFile/" OldFile*
I simply wanted the same thing with a copy command:
copy "s/OldFile/NewFile/" OldFile*
So here it is!
Installation: Put it in /usr/local/bin or elsewhere on your path.
#!/usr/bin/perl -w
#
# This script is derived from the rename unix utility by Robin Barker,
# which itself is based on Larry Wall's original script eg/rename
# from the perl source.
#
# This script is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
# $Log: copy,v $
# Revision 1.0 2018/04/06 16:16:31 jward
# created copy script
#
use strict;
use File::Copy;
use Getopt::Long;
Getopt::Long::Configure('bundling');
my ($verbose, $no_act, $force, $op);
die "Usage: copy [-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 copied: $_ already exists\n";
}
elsif ($no_act or copy $was, $_)
{
print "$was copied as $_\n" if $verbose;
}
else
{
warn "Can't copy $was $_: $!\n";
}
}
__END__
=head1 NAME
copy - copies multiple files
=head1 SYNOPSIS
B<copy> S<[ B<-v> ]> S<[ B<-n> ]> S<[ B<-f> ]> I<perlexpr> S<[ I<files> ]>
=head1 DESCRIPTION
C<copy>
copies the filenames supplied according to the rule specified as the
first argument.
The I<perlexpr>
argument is a Perl expression which is expected to modify the C<$_>
string in Perl for at least some of the filenames specified.
If a given filename is not modified by the expression, it will not be
copied.
If no filenames are given on the command line, filenames will be read
via standard input.
For example, to copy all files matching C<*.bak> to C<*.tmp>,
you might say:
copy 's/\.bak$/.tmp/' *.bak
To copy a set of source files (e.g. MyCode.c and MyCode.h) to a new
set of source files (MyNewCode.c and MyNewCode.h, repsectively),
you could use:
copy 's/MyCode\./MyNewCode./' MyCode.*
=head1 OPTIONS
=over 8
=item B<-v>, B<--verbose>
Verbose: print names of files successfully copied.
=item B<-n>, B<--no-act>
No Action: show what files would have been copied.
=item B<-f>, B<--force>
Force: overwrite existing files.
=back
=head1 ENVIRONMENT
No environment variables are used.
=head1 AUTHOR
Larry Wall
=head1 SEE ALSO
mv(1), perl(1)
=head1 DIAGNOSTICS
If you give an invalid Perl expression you'll get a syntax error.
=head1 BUGS
The original C<copy> did not check for the existence of target filenames,
so had to be used with care. I hope I've fixed that (Robin Barker).
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment