Delete files returned by find

From MyLabWiki
Revision as of 12:10, 8 February 2011 by Alex (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.