I'm trying to delete all empty folders within a directory. However, find . -type f -empty does not find anything because every folder contains a hidden .svn folder.

How can I work around this?

link|improve this question
Are the .svn directories empty? – Dennis Williamson Feb 25 '11 at 15:31
No, they are not. I think Ollis solution will work. However, I did not test it yet. – atamanroman Feb 25 '11 at 15:53
feedback

3 Answers

up vote 0 down vote accepted

If you can, you can off course first remove all .svn folders. Downside: you'll lose version control information, if someone is using SVN. If someone is using SVN, then it's not good idea to just remove those folders (or actually you have to remove those from SVN too, as SVN is tracking folders and files).

If that's not possible, I would go with scripting route:

for folder in $(find . -type d); do
 if [ "`ls $folder | wc -l`" -eq 0 ]; then
  echo "I am going to delete $folder"
 fi
done

First try test-run, because there might be something surprising. Then you can change rm -r instead of that echo.

Note however, this will remove all folders with only dot-files (so for example a/.this_is_super_important will be deleted, if there is no other files or folders).

link|improve this answer
feedback

I just stumbled upon this and I used this script but modified it to properly consider empty dirs only the ones with .svn and no other file, hidden or normal.

Also, bear in mind that I am using svn rm, so this is suppose to work against a repository, you could adapt the script to do something different on the directory.

for folder in $(find -type d ! -path *.svn*); do
  if [ "`find $folder ! -path *.svn* ! -path $folder | wc -l`" -eq 0 ]; then
    svn rm $folder
  fi
done
link|improve this answer
feedback

Minor modification (simply print the directory and pipe into xargs later):

for folder in $(find -type d ! -path *.svn*); do
  if [ "`find $folder ! -path *.svn* ! -path $folder | wc -l`" -eq 0 ]; then
    echo $folder
  fi
done

Put this somewhere in your $PATH, maybe as svn-empties and then first run:

svn-empties

to list which directories it found (modify list if necessary) and then pipe into xargs:

svn-empties | xargs svn rm

This script has become very useful to me while using git-svn (thank you!). Since git does not track empty directories I can end up with many empty directories in the Subversion repository. I svn co a separate copy of the repository and run svn-empties | xargs svn rm periodically. Using Subversion in the first place was not my decision ;)

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.