Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: how do I recursively find and remove empty directories?

Tags:

file

ruby

I'm trying to write some ruby that would recursively search a given directory for all empty child directories and remove them.

Thoughts?

Note: I'd like a script version if possible. This is both a practical need and a something to help me learn.

like image 662
Dane O'Connor Avatar asked Aug 17 '09 21:08

Dane O'Connor


People also ask

How do you delete an empty directory in recursively?

First, search all the empty files in the given directory and then, delete all those files. This particular part of the command, find . -type f -empty -print, will find all the empty files in the given directory recursively. Then, we add the -delete option to delete all those files.

Can rm remove empty directories?

Removing Directories with rm rm is a command-line utility for deleting files and directories. Unlike rmdir the rm command can delete both empty and non-empty directories. By default, when used without any option rm does not remove directories.

What is the command used to remove the empty directories?

Use the rmdir command to remove the directory, specified by the Directory parameter, from the system. The directory must be empty (it can contain only .


1 Answers

Why not just use shell?

find . -type d -empty -exec rmdir '{}' \;

Does exactly what you want.

like image 99
koenigdmj Avatar answered Sep 16 '22 11:09

koenigdmj