#!/usr/bin/perl -w ######################################################################### # # # Clean up a directory tree # # Copyright (C) 2001-04, John Zaitseff # # # ######################################################################### # Author: John Zaitseff # Date: 3rd December, 2004 # Version: 2.4 # This script removes "unwanted" files from a directory and all its # sub-directories. These files are typically backup files with the # following patterns: *~ #* .#* ,* *.bak *.BAK # # Syntax: # rmbak [options] [directory ...] # # where the options may include: # # -n, --dry-run - Perform a dry run (does not actually do the operation). # -q, --quiet - Do not print filenames when deleting them. # -h, --help - Show a command-line summary. # -V, --version - Show program version information. # # If "directory" is not specified, the current working directory is used. # This program is free software. You may distribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation; either Version 2 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, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ######################################################################### # Configuration parameters and default values use strict; # Enforce better programming habits use File::Find qw(find); # "find" does all the hard work (our $O = $0) =~ s,^.*/,,; # Script name (without path) our $version = "2.4"; # Script version # Delete pattern: *~ #* .#* ,* *.bak *.BAK our $delpat = '.*/(?i:.*~|\.?#.*|,.*|.*\.bak)\z'; # Function prototypes sub showusage(); sub showversion(); sub showcmdlerr(@); sub deletefile($); ######################################################################### # Initialise global variables our $opt_dry = 0; # Perform a dry run? our $opt_quiet = 0; # Be quiet when deleting files? ######################################################################### # Main program # Process command line arguments while ($_ = $ARGV[0]) { last if (! /^-/); shift @ARGV; last if ($_ eq '--'); # Split combined short-form options into single arguments if (/^-(\w{2,})/) { my @args = split //, $1; foreach my $arg (@args) { $arg = "-$arg"; } unshift @ARGV, @args; next; } # Process command-line options if (($_ eq "--dry-run") || ($_ eq "-n")) { $opt_dry = 1; } elsif (($_ eq "--quiet") || ($_ eq "-q")) { $opt_quiet = 1; } elsif (($_ eq "--help") || ($_ eq "-h") || ($_ eq "-?")) { showusage(); } elsif (($_ eq "--version") || ($_ eq "-V")) { showversion(); } else { showcmdlerr("Unrecognised option: $_"); } } # Use the current directory if no directories are specified if ($#ARGV == -1) { @ARGV = ("."); } # Check that each argument is a directory or a link to a directory foreach (@ARGV) { if (! -e $_) { die "$O: $_: $!\n"; } elsif (! -d _) { die "$O: $_: Not a directory\n"; } } # Do the actual search, finally find({ wanted => \&deletefile, no_chdir => 1}, @ARGV); exit(0); sub deletefile($) { my $ret = 1; if (-f $_ || -l $_) { if (/$delpat/o) { # Filename matches the deletion pattern $ret = unlink($_) if ! $opt_dry; if ($ret != 1) { warn "$_: Cannot remove file: $!\n"; } else { print "$_\n" if ! $opt_quiet; } } } } ######################################################################### # Display usage information and terminate sub showusage() { print <<"DATAEND" $O v$version: Clean up a directory tree. Copyright (C) 2001-04, John Zaitseff. This program removes "unwanted" files from a directory and all its sub-directories. These files are typically backup files with the following patterns: *~ #* .#* ,* *.bak *.BAK Syntax: $O [options] [directory ...] where the options may include: -n, --dry-run - Perform a dry run (does not actually do the operation). -q, --quiet - Do not print filenames when deleting them. -h, --help - Show a command-line summary. -V, --version - Show program version information. If "directory" is not specified, the current working directory is used. DATAEND ; exit(0); } ######################################################################### # Display program version information and terminate sub showversion() { print <<"DATAEND" $O v$version: Clean up a directory tree. Copyright (C) 2001-04, John Zaitseff. This program, including associated files, is distributed under the GNU General Public License. See the file COPYING for more information. DATAEND ; exit(0); } ######################################################################### # Show an error message relating to the command-line and terminate sub showcmdlerr(@) { map { warn "$O: $_\n" } @_; die "\nUsage:\n" . " $O [-nq] [directory ...] - to remove unwanted files\n" . " $O --help - to display more help\n"; }