Tuesday, August 9, 2011

Find and delete into directories recursively

How to find a set of files or directories that are named the same?  For example, to delete all the '.svn' directories in the current working directory.

One easy way is
rm -rvf `find . -type d -name .svn`

The option [-v] prints out the files/directories found.  and rm -rf is the typical force deletion of a directory.

Another way to do it is
find . -name ".svn" -exec rm -rf {} \;

But this command prints out warning messages that sometimes confuse users.
find: ./.svn: No such file or directory
find: ./images/.svn: No such file or directory

The warning message means it found a .svn directory in the current directory and deleted as instructed ( -exec rm -rf ).  When the command find wanted to descend into .svn directory, it could not.

The trailing {} \; is required.  Pay attention to its format: there is a space between } and \.

No comments:

Post a Comment