Remove all Vim undo files in all but one directory

findrmvimwildcards

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:

.myfile.txt.un~

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!

Best Answer

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

Related Question