TLDR
find . -name .svn -prune -execdir rm -rf {} +
Details
Use -execdir
, not -exec
From man find
:
There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.
In most case, -execdir
is a drop-in replacement for -exec
.
Use +
, not ;
From man find
:
As with the -exec action, the `+' form of -execdir will build a command line to process more than one matched file, but any given invocation of command will only list files that exist in the same subdirectory.
When looking for an exact name match, +
and ;
will do the same, as you cannot have two files with the same name in the same directory, but +
will provide increased performance when several files/directories match your find expression within the same directory.
Also, ;
needs escaping from your shell, +
does not.
Use -prune
From man find
:
-prune
: True; if the file is a directory, do not descend into it.
This avoid searching a directory that we want to delete. Obviously, it needs to be put after the name test. See:
$ mkdir -p test/foo/bar
$ find test -name foo -execdir rm -rf {} +
find: ‘test/foo/bar’: No such file or directory
Versus:
$ mkdir -p test/foo/bar
$ find test -name foo -prune -execdir rm -rf {} +
# no error
-delete
option. – Marco Sep 09 '13 at 08:49-exec rm -r "{}" \;
to the end of the find - be careful when usingrm -r
! :) – Drav Sloan Sep 09 '13 at 08:52-name ".svn"
it matches only the.svn
directory itself and not the files located in the.svn
directory. – Marco Sep 09 '13 at 09:22rm -r \
find . -name ".svn"`` also works – Arnaud Sep 09 '13 at 09:31-exec
with quoted"{}"
). – Drav Sloan Sep 09 '13 at 09:55rm -r
– Drav Sloan Sep 09 '13 at 09:56rm -r
withrmdir
(rmdir will delete empty directories, but fail with errors on directories containing files). – Drav Sloan Sep 09 '13 at 10:08{}
in quotes has no effect. – G-Man Says 'Reinstate Monica' Oct 27 '20 at 23:43