As a Vim user, I sometimes have a buildup of unremoved swap files (
.swp) floating around in my directories. Here’s how I find and delete these pesky files.
First, I use
find to list out all the swap filepaths:
$ find . -name '*.swp'
You can see that it’s traversing the current directory
. and trying to match the pattern
Before deleting the swap files, make sure to quit Vim so swap files currently in use aren’t accidentally deleted.
To delete the files, all you need to do is pass the
-delete option (warning: this operation cannot be undone):
$ find . -name '*.swp' -delete
To be safe and target only file types, you can pass
$ find . -type f -name '*.swp' -delete
To ignore files in the
./dist directory, pass
-path ./dist -prune -o and
$ find . -path ./dist -prune -o -name '*.swp' -print -delete
./dist directory is excluded from the
Your directory should now be pruned of swap files!