Skip to content

Instantly share code, notes, and snippets.

@vifon
Created August 11, 2012 19:38
Show Gist options
  • Save vifon/3326648 to your computer and use it in GitHub Desktop.
Save vifon/3326648 to your computer and use it in GitHub Desktop.
mvln
#!/usr/bin/env perl
=head1 NAME
mvln - move a file and leave a symlink to its new location in its place
=head1 SYNOPSIS
B<mvln> I<source> I<destination>
B<mvln> I<sources...> I<destination_directory>
B<mvln> I<--help>
=head1 AUTHOR
Wojciech 'vifon' Siewierski <darkvifon at gmail dot com>
=head1 COPYRIGHT
Copyright (C) 2012 Wojciech Siewierski
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=cut
use warnings;
use strict;
use File::Basename;
use File::Spec;
use File::Copy qw(mv);
use Pod::Usage;
unless (@ARGV == 0) {
pod2usage({ -verbose => 2 }) if $ARGV[0] eq "--help"
or $ARGV[0] eq "-h";
}
pod2usage({ -verbose => 1 }) if @ARGV < 2;
my $destination = pop @ARGV;
if (@ARGV > 1 && not -d $destination) {
die "\"$destination\" is not a directory\n"
}
$destination = File::Spec->rel2abs($destination);
for my $file (@ARGV) {
my $destination_file;
if (-d $destination) { # get path to file created in the specified directory
$destination_file = File::Spec->catdir($destination,
basename($file));
} else {
$destination_file = $destination;
}
mv $file, $destination_file;
symlink $destination_file, $file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment