sysclean.pl 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/perl
  2. # vi: expandtab shiftwidth=4:
  3. use strict;
  4. use warnings;
  5. use Getopt::Std;
  6. sub usage {
  7. print "usage: $0 [-hnr]\n";
  8. print "\t-h\tPrint this help\n";
  9. print "\t-n\tPrint commands, don't execute\n";
  10. print "\t-r\tRemove directories\n";
  11. exit(1);
  12. }
  13. our ( $opt_r, $opt_n, $opt_h );
  14. my $user_pattern = '^@user';
  15. my $group_pattern = '^@group';
  16. getopts('hnr')
  17. or usage();
  18. usage if $opt_h;
  19. if ($<) {
  20. print "not root, just printing what would be done\n";
  21. $opt_n = 1;
  22. }
  23. while (<>) {
  24. chomp;
  25. if (m/$user_pattern/x) {
  26. my @token = split /[\s:]/x, $_;
  27. my $user = $token[1];
  28. my @cmd = ( '/usr/sbin/userdel', "-v", $user );
  29. print join( ' ', @cmd ), "\n";
  30. system @cmd unless $opt_n;
  31. }
  32. elsif (m/$group_pattern/x) {
  33. my @token = split /[\s:]/x, $_;
  34. my $group = $token[1];
  35. my @cmd = ( '/usr/sbin/groupdel', "-v", $group );
  36. print join( ' ', @cmd ), "\n";
  37. system @cmd unless $opt_n;
  38. }
  39. else {
  40. my $file = $_;
  41. if ( -d $file ) {
  42. if ($opt_r) {
  43. print "delete directory ", $file, "\n";
  44. rmdir "$file"
  45. or warn "error removing directory $file: $!\n";
  46. }
  47. else {
  48. print "keeping directory ", $file, "\n";
  49. }
  50. }
  51. else {
  52. print "delete file ", $file, "\n";
  53. unlink $file unless $opt_n;
  54. }
  55. }
  56. }