http://qs1969.pair.com?node_id=210421

   1: #!/usr/bin/perl
   2: #rembak.pl
   3: #it is useless, but i like Perl to do things for me (-:
   4: 
   5: use Getopt::Long;
   6: 
   7: my $verbose_mode = '';
   8: GetOptions('verbose' => \$verbose_mode);
   9: 
  10: $dirname = `pwd`;
  11: chomp $dirname;
  12: 
  13: $counter = 0;
  14: open(SIN, "ls *~|") or die "Unable to read list of files!\n";
  15: 	while(<SIN>){
  16: 		chomp;
  17: 		system("rm -f $_");
  18: 		if ($verbose_mode) { print "\t$_ deleted.\n"; }
  19: 		$counter++;
  20: 	}
  21: close SIN;
  22: print "I have deleted $counter backup files in directory $dirname.\n"

Replies are listed 'Best First'.
Re: remove backups
by grinder (Bishop) on Nov 05, 2002 at 11:57 UTC

    Your script seems to be half way between being able to remove backup files from the current directory or an explicit directory (maybe passed in from the command line).

    Here's how I would remove backup files from the current directory, without relying on external programs.

    #! /usr/bin/perl -w use strict; use Getopt::Long; my $verbose_mode = ''; GetOptions('verbose' => \$verbose_mode); my $counter = 0; opendir DIR, '.' or die "Cannot open . for directory input: $!\n"; while( defined( my $entry = readdir(DIR) )) { next unless $entry =~ /~$/; if( unlink $_ ) { ++$counter; print "\t$_ deleted.\n" if $verbose_mode; } else { warn "Could not unlink $_: $!\n"; } } closedir DIR; print "I have deleted $counter backup files in current directory.\n"

    I'll leave it as an exercise to the reader to figure out how to extend this to work on any directory. Disclaimer: I have only checked that this code is syntactically correct. It may not be semantically correct.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
      Just regards
      pfm="";
Re: remove backups
by smitz (Chaplain) on Nov 05, 2002 at 11:32 UTC
    Hmmm...

    • You chomp $dirname - why?
    • You define the scalar $dirname - why?
    • You use a system call to rm, making it platform dependant. Why not use unlink?
    • You empty the entire directory from which this is run - why not use rm * | del *.* | whatever?

    Just some friendly critique

    SMiTZ

    Update: I Sir, am a plonker.

      ... chomp $dirname - why?

      To get rid of that pesky newline. (Yes, there is one.)

      ... define the scalar $dirname - why?

      That he may print it (in the last line)?

      ... use a system call to rm, making it platform dependant. Why not use unlink?

      Opening "ls *~|" already makes it platform dependent. Of course, that could be glob("*~"), so your question still stands ...

      ... empty the entire directory from which this is run - why not use rm * | del *.* | whatever?

      I think you miss the point: the alternative would be rm *~, which is quite easy to get wrong. Or at least easy enough to make guys like me paranoid.

      Me, I have a bash function for this sort of thing:

      function cleanup { rm "$@" *~ .*~ #* }

      If it ever gets complicated enough that I make a Perl script out of it, I'll probably make it platform independant. For now, it does the job.

      The Sidhekin
      print "Just another Perl ${\(trickster and hacker)},"

        Undeserved Abbots are quick to reply, slow to think. Of course pwd is a command, not a directory name. The backticks should have given it away, but its one of those days|weeks|lives.
        Apologies to pfm, you know where the -- button is...

        SMiTZ