#!/usr/bin/perl # # tarj This script creates a compressed tarfile of a specified file. # # The command is used in this way: # # tarj [-delete | -extension file-ext] # # Revision History # 1.0 Initial revision. 131105 # 1.1 Ensure output file doesn't exist; 131202 # proper removal of trailing slashes. # 1.2 Additional pod added. 140205 # 1.3 Added licensing info. 180531 # # Written by Wayne Morrison, 131105. # # Copyright 2013 Wayne Morrison # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # use strict; use Getopt::Long qw(:config no_ignore_case_always); # # Version information. # my $NAME = "tarj"; my $VERS = "$NAME version: 1.3"; ############################################################################ # # Options fields. # my %opts = (); # Options. # # Command line arguments. # my @opts = ( 'delete', # Delete the originals. 'extension=s', # File extension for output file. 'verbose', # Give verbose output. 'help', # Give a help message. 'Version', # Display the program version. ); my $delflag = 0; my $ext = "tbz2"; my $verbose = 0; ############################################################################ main(); exit(0); #----------------------------------------------------------------------------- # Routine: main() # # Purpose: Driver routine. # sub main { $| = 1; # # Munch on the options and arguments. # optsandargs(); # # If arguments were given, we'll translate those files. # foreach my $node (@ARGV) { tarbuzz($node); } } #---------------------------------------------------------------------- # Routine: optsandargs() # # Purpose: Parse the command line for options and arguments. # sub optsandargs { # # Parse the options. # GetOptions(\%opts,@opts) || usage(); # # Check for some immediate-action options. # usage() if(defined($opts{'help'})); version() if(defined($opts{'Version'})); $delflag = $opts{'delete'}; $verbose = $opts{'verbose'}; # # Get the file extension and ensure it doesn't contain any # directory separators. # $ext = $opts{'extension'} if(defined($opts{'extension'})); if($ext =~ /\//) { print STDERR "The file extension cannot contain slashes.\n"; exit(2); } # # Ensure some files were given. # usage() if(@ARGV == 0); } #---------------------------------------------------------------------- # Routine: tarbuzz() # # Purpose: This routine creates the new compressed tarball of a given # input file or directory. Trailing slashes are removed from # the input file (or directory), the input file must exist and # the output file must not exist. After creating the tarball, # the input file is deleted if the -delete option was given. # sub tarbuzz { my $fn = shift; # File to translate. my $out = ''; # Output file. my $ret; # Return code. # # Remove trailing slashes. # $fn =~ s/\/+$//; # # Ensure the file exists. # if(! -e $fn) { print STDERR "\"$fn\" does not exist\n"; return; } # # Create the output file name. # $out = "$fn.$ext"; # # Ensure the output file doesn't exist. # if(-e $out) { print STDERR "output file \"$out\" exists\n"; return; } # # Create a compressed tarball of the file. # system("tar -cjf $out $fn"); $ret = $? >> 8; # # If the tarball failed, give an error message. # If the tarball was created and the -verbose flag was set, # give a success message. # if($ret == 0) { if($verbose) { print "\"$fn\" -> \"$out\" created\n"; } } else { print STDERR "problem creating \"$fn\" tarfile -- return code $ret\n"; } # # Get rid of the original if the -delete flag was given. # if($delflag) { system("rm -fr $fn"); $ret = $? >> 8; if($ret == 0) { if($verbose) { print "original file \"$fn\" deleted\n"; } } else { print STDERR "problem deleting original file \"$fn\" -- return code $ret\n"; } } } #---------------------------------------------------------------------- # Routine: version() # # Purpose: Print the version number(s) and exit. # sub version { print STDERR "$VERS\n"; exit(0); } #---------------------------------------------------------------------- # Routine: usage() # # Purpose: Give usage message and exit. # sub usage { print STDERR "usage: tarj [options] \n"; print STDERR "\n"; print STDERR "\twhere [options] are:\n"; print STDERR "\t\t-delete\n"; print STDERR "\t\t-extension \n"; print STDERR "\t\t-verbose\n"; print STDERR "\t\t-help\n"; print STDERR "\t\t-Version\n"; exit(0); } 1; ############################################################################## =pod =head1 NAME B - creates a compressed tarfile of a specified file =head1 SYNOPSIS tarj [options] =head1 DESCRIPTION B creates a compressed tarfile of a specified file or directory. B is a shorthand command for B; additions are briefly described below. The B command is used to create the tarfile; the I<-j> option is used to have B compress the tarfile. Compression is done with the B compression format. The compressed tarfile is named according to the original source file. The compressed tarfile is the original file's name with a file extension appended. The default extension is B, but an alternate extension may be specified with the I<-extension> option. The original file will be deleted if the I<-delete> option is given. If it isn't given, then the original file will be kept in place. As stated above, B is a shorthand command for the B command. The additional functionality B provides includes: it is quicker to type a B command than to type the required B command, slashes (inserted by some shell file-extension commands) are removed from the input name, the input file may optionally be deleted after tarball creation, and some filename checking is provided. =head1 OPTIONS B takes the following options: =over 4 =item I<-delete> This flag indicates that the original file should be deleted after it has been tarred and compressed. =item I<-extension> This flag indicates the extension that should be used for the output file. The default extension is "B<.tbz2>" if this option is not specified. =item I<-verbose> Give verbose output. =item I<-Version> Display the version information for B. =item I<-help> Display a help message. =back =head1 AUTHOR Wayne Morrison, wayne@waynemorrison.com =head1 LICENSE Copyright 2013 Wayne Morrison Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =head1 SEE ALSO B, B =cut