I use vim a lot, and my area has power failure a lot. So the resultant is I get many *.swp files scattered over my PC.

I want an alias of rm command that removes all files with either .swp, ~, .netrwhist, .log or .bak extensions system wide (or atleast in my home directory). The command should delete the files system wide/home directory even when I am on ~/Desktop.

How can I implement so?

  • How about: find ./ -name \*\~ | xargs /bin/rm – Emanuel Berg Aug 18 '12 at 16:58
  • Also, if the ~ files bug you, alias ls to ls -B. At least you won't see them. – Emanuel Berg Aug 18 '12 at 17:00
  • @EmanuelBerg find ./ -name \*\~ | xargs /bin/rm does not works when I am on ~/Desktop and there is any ~ file in home directory. See my question, what I said. – Santosh Kumar Aug 18 '12 at 17:24
  • 3
    Three words: Uninterruptible power supply. I don't turn on a computer without one. – cjm Aug 19 '12 at 6:27
  • 2
    @Santosh, then you need a better UPS. A minimum runtime should be 5 minutes. 8 seconds doesn't even give you time to shut down the computer (as you've obviously noticed). – cjm Aug 19 '12 at 15:57

This will delete all the files with a name ending in .swp, ~, .netrwhist, .log or .bak anywhere under your home directory. No prompt, no confirmation, no recovery, the files are gone forever.

find ~ -type f \( -name '*.swp' -o -name '*~' -o -name '*.bak' -o -name '.netrwhist' \) -delete

(I purposefully omit *.log because it sounds dangerous, this is not a common extension for temporary files and there are plenty of non-temporary files with that name.)

If your OS isn't Linux, replace -delete by -exec rm {} +.

You should perhaps configure Vim to put its swap files in a single directory by setting the directory option:

set dir=~/tmp/vim-swap-files//,/var/tmp//

Create the directory first. The // at the end makes the swap file name include the directory location of the original file, so that files with the same name in different directories don't cause a crash.

You can do the same thing for backup files with the backupdir option, though it makes a lot less sense.

If you use Emacs, set auto-save-file-name-transforms to point every file to a single directory.

(setq auto-save-file-name-transforms
      '("\\`.*\\'" "~/tmp/emacs-auto-save-files/\\&" t))

If you're targetting vim files specifically, I would recommend you use the backupdir variable. :h backupdir says:

'backupdir' 'bdir'      string  (default for Amiga: ".,t:",
                                 for MS-DOS and Win32: ".,c:/tmp,c:/temp"
                                 for Unix: ".,~/tmp,~/")
                        {not in Vi}
        List of directories for the backup file, separated with commas.
        - The backup file will be created in the first directory in the list
          where this is possible.  The directory must exist, Vim will not
          create it for you.
        [... snip ...]

I use this line in my .vimrc:

set backupdir=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp

Also you need to create ~/.vim-tmp directory if it already doesn't exists, because doesn't creates that for you.

  • OK, that's useful. But gedit generates *~ files. – Santosh Kumar Aug 18 '12 at 17:04

Short Answer Deletes temporals which ends with ~

find . -type f \( -iname "*~" \) -delete

Or others like .swap etc ..

find . -type f \( -iname "*.swp" \) -delete

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.