How to remove files using find’s multiple -name parameter

bashcommand linefindshellsyntax

How to remove files, containing tilde in the filename or extension, recursively?

For example, files of the vim, with the names like .my_file.c.un~?

I'm using this find sequence for that:

find . -name "*.un~" -o -name "*.swo" -o -name "*.swp" -exec rm -f {} \;

But it doesn't remove the files. Still running just a pure find displaying a list of the files correctly:

./.my_file.c.un~
./.my_file.c.swp
./.file2.c.un~

Also, removing them with the pure rm -f .my_file.c.un~ works perfectly. Changing the -exec rm -f {} \; into the -delete still doesn't help.

Best Answer

When you use multiple logical operations, you need to group them by brackets as below:

find . \( -name "*.un~" -o -name "*.swo" -o -name "*.swp" \) -delete

where you have to either backslash or quote the brackets ('(' ... ')') to avoid parsing these special characters by the shell.

In above example I'm using -delete instead of -exec rm -f {} which automatically remove the file, so you don't need to worry about files with spaces, otherwise it could end up bad for you.

For more syntax examples, check man find.

See also:

Related Question