Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to remove all vim swap file *.swp and remove them with a confirmation. The find command found the files, but rm says No such file or directory with the -i option. When I hardcode the path of the file and just use rm -i then it seems to work.

See below

(doors)hobbes3@hobbes3 ~/Sites $ find mysite mysite_BAK -name *.swp -exec 'rm -i {}' \;
find: rm -i mysite/templates/.base.html.swp: No such file or directory
find: rm -i mysite/templates/doors/orders/.create.html.swp: No such file or directory
find: rm -i mysite/templates/doors/orders/.detail.html.swp: No such file or directory
find: rm -i mysite/templates/doors/orders/.list.html.swp: No such file or directory
(doors)hobbes3@hobbes3 ~/Sites $ rm -i mysite/templates/.base.html.swp 
remove mysite/templates/.base.html.swp? n

I guess I'm doing something wrong with the -exec option. Any suggestions? Thanks in advance!

share|improve this question
Actually I just tried without the -i option and it still gave the same errors. Hmmm... – hobbes3 Mar 25 '12 at 7:41
up vote 7 down vote accepted

The error is generated by find, not rm.

The reason is that you have written it so 'rm -i <file>' is the single argument. This shall be rewritten:

find mysite mysite_BAK -name '*.swp' -exec rm -i '{}' \;

so find gets multiple arguments after "-exec" and treats the first one as command and others as the command arguments.

share|improve this answer
A simple solution. Thanks. – hobbes3 Mar 25 '12 at 7:54
You can also leave out the quotes around {} – Bernhard Mar 25 '12 at 9:17
Good practice to use "*.swp" instead of just *.swp; The latter doesn't work in zsh, because of how the shells treat wildcards differently. – metasoarous Nov 11 '15 at 17:04
@metasoarous thanks, it's also useful. I corrected initially only spec for '{}' but not rest of line. Zsh isn't the only for this; my bash will also fail due to "failglob" option in my default config. – Netch Nov 12 '15 at 14:53

I usually find it easier to use simple tools combined

cd to dir and then:

rm -i `find . | grep .swp$`

its easier to remember (if performance is not an issue)!

WARNING: Only use this if your files don't contain spaces. If they do then use the answer above!

share|improve this answer
Wow, this is really easy and helpful at the same time! Thanks! I will use this from now on instead of complex find – Karel Bílek May 26 '15 at 9:35
This will fail if any subdir name or filename found contains spaces. – Dubu Nov 12 '15 at 15:42
@Dubu True, i will edit to reflect that – polvoazul Nov 20 '15 at 4:21

In your case, I would run,

find mysite mysite_BAK -type f -name .\*.sw? -exec rm -i {} ";"

VIM's swap file does not only end in .swp. If .swp exists, VIM would create .swo, then .swn, and so on. Also, swap files are prefixed with a dot (.), e.g., .file1.rb.swp

Other ways to find and delete VIM's swap files, without the confirmation (-i):

find some_dir -type f -name .\*.sw? | xargs rm 
find some_dir -type f -name .\*.sw? -exec rm {} ";"
find some_dir -type f -name .\*.sw? -delete  # -delete option does not work in Git Bash

Without "-type f", you would get an error when deleting a directory. Also, the \ in .\*.sw? makes it compatible in multiple platforms.

share|improve this answer

Your Answer


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.