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.-exec rm -r "{}" \;
to the end of the find - be careful when usingrm -r
! :)-exec
with quoted"{}"
).{}
in quotes has no effect.