Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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
1  
Three words: Uninterruptible power supply. I don't turn on a computer without one. –  cjm Aug 19 '12 at 6:27
1  
@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
show 4 more comments

2 Answers

up vote 8 down vote accepted

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))
share|improve this answer
    
Thanks, it works as I wished it to be. –  Santosh Kumar Aug 18 '12 at 22:28
    
+1 this is clever, thanks –  Anthony Aug 19 '12 at 10:20
add comment

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,~/")
                        global
                        {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.

share|improve this answer
    
OK, that's useful. But gedit generates *~ files. –  Santosh Kumar Aug 18 '12 at 17:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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