I just realized that I have tons of Vim undo (.un~) files sprinkled around my file system. I'd like to delete all of these files except in one directory—~/.tmp. My first problem is that I can't seem to find a Unix command to delete these things. For example, I have a file name that looks like this:


I've tried, rm -f *.un, rm -f *.un\~, rm -f *.un*, etc. and I can't seem to find any command that can delete these files. How can I delete these files?

Secondly, I'd like to write a command with find that can visit all my directories and delete these files, with the exception of the ~/.tmp directory. I'm quite afraid of executing this command incase it's wrong. Can anyone help be construct a find command to do this? Thanks in advance for the help!

  • You can keep all the undo files in one directory by setting undodir in .vimrc
    – goldilocks
    Feb 27 '13 at 16:23

These commands didn't work because wildcard patterns omit dot files (files whose name begins with the character .) unless the dot appears explicitly in the pattern. So *.un~ matches yourfile.txt.un~ but not .myfile.txt.un~, whereas .*.un~ does match .myfile.txt.un~.

You should be able to use find(1) for this (find wildcard matching doesn't treat dot files specially):

find / -name "*.un~" -not -path "~/.tmp/*" -delete

That tells find to search / for all files matching *.un~ that aren't in ~/.tmp and delete them. If you take off -delete it will just output a list, so you can check and make sure it's not going to delete the wrong things. You also might want to throw -mount in there to stop it from searching other filesystems you have mounted


* does not expand dot files. You'll have to say e.g.:

rm ./.*.un~

If you use find, which probably would be the easiest, I'd use

find -iname ".*.un~" ...

instead of:

find -iname "*.un~" ...

As the latter would match foo.un~ as well as .foo.un~.

And, yes, do a dry run first to be sure you have the correct matches.

  • Thanks. I didn't realize that wildcards do not expand after dotfiles. Much appreciated.
    – turtle
    Feb 27 '13 at 17:30
  • @turtle You can click the checkmark next to an answer to accept it Mar 2 '13 at 7:25

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.