TLDR
find . -depth -name .svn -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 -depth
From man find
:
-depth
: Process each directory's contents before the directory itself.
This avoid searching a directory after deleting it. 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 -depth -name foo -execdir rm -rf {} +
# no error
-delete
option.-exec rm -r "{}" \;
to the end of the find - be careful when usingrm -r
! :)-name ".svn"
it matches only the.svn
directory itself and not the files located in the.svn
directory.-exec
with quoted"{}"
).