Deleting unwanted Vim swap files using Perl
Published by Matthew Daly at 29th October 2010 8:49 pm
Yesterday I realised that I had somehow managed to scatter Vim swap files all across the Dropbox folder I use to share Perl and Python scripts I'd written between several computers, and it would be a good idea to clear them up. I didn't like the idea of using grep to search for them and manually deleting them, so I decided this was the ideal opportunity to write a Perl script to do it for me!
I came up with the following:
1#!/usr/bin/perl -w23use strict;4use Cwd;56sub searchDir7{8 # Subroutine to scan a directory looking for Vim swap files9 # Get directory to read and current directory10 my $readdir = shift;11 my $startdir = cwd();1213 # Change directory to the target one14 chdir($readdir) or die "Unable to open $readdir! $!\n";15 print "Scanning contents of directory $startdir\n";1617 # Open the directory and grab the names of all the files and folders in it18 opendir(DIR, ".") or die "Unable to open current directory! $!\n";19 my @entries = readdir(DIR) or die "Unable to read directory! $!\n";20 closedir(DIR);2122 # Loop through the files and folders in the directory23 foreach my $entry (@entries)24 {25 # Skip this one and the one above it in the filesystem hierarchy26 next if($entry eq ".");27 next if($entry eq "..");2829 # If a file is a directory, call the searchDir subroutine recursively in order to scan it30 if(-d $entry)31 {32 searchDir($entry);33 next;34 }3536 # Use a regular expression to check to see if the current file starts with a period, and ends with .swp - if it does, it's a Vim swap file37 if($entry =~ m/^\..*\.swp$/)38 {39 # Inform the user that a Vim swap file has been found and print out the path to it40 print "Found a Vim swap file!\n";41 my $swppath = cwd();42 print "It's the file $entry in $swppath.\n";43 my $fullpath = $swppath . "/" . $entry;44 print "The full path is $fullpath.\n";4546 # Prompt the user to delete the file47 print "Do you wish to delete this file? (Y/N)\t";48 chomp(my $reply = );49 if($reply =~ m/y/i)50 {51 print "Deleting $fullpath...\n";52 unlink($fullpath);53 }54 }55 }5657 chdir($startdir);58}5960# Get directory to begin the search61print "Enter directory to start search: ";62chomp(my $beginSearch = );6364# call searchDir to start the search65searchDir($beginSearch);
Thankfully, I've now discovered the Preserve Code Formatting plugin for WordPress, which seems to do a good job at making the code look presentable!
This isn't perfect - it uses recursion to examine subdirectories, and when I ran it on my /home folder it somehow wound up in /sys on my Ubuntu machine and I ended up getting a deep recursion warning (a little research suggests this happens when it goes over 100 directories in). However, it seems to work fine for scanning individual folders in my /home directory, and that's all I really wanted anyway.
I love how Perl makes writing this kind of simple script so easy. It's a great language for that kind of systems administration task.