Delete files returned by find

From MyLabWiki
Jump to: navigation, search

Task: Find all files whose name contains a specific pattern and delete them.

Finding the files (or folders) is trivial using find[1] – however, we can not simply pipe the output of find to rm because rm[2] does not read input from standard input.

The solution is to use xargs[3]. The example below finds all files and folders containing ".svn" and deletes them:

 find -name .svn -print0 | xargs -0 rm -f -r

The -print0 option of find causes each entry to be a 0-terminated string rather than EOL terminated. It is consistent with the -0 option of rm.

References

  1. http://unixhelp.ed.ac.uk/CGI/man-cgi?find
  2. http://unixhelp.ed.ac.uk/CGI/man-cgi?rm
  3. http://unixhelp.ed.ac.uk/CGI/man-cgi?xargs