Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding empty directories

I need to find empty directories for a given list of directories. Some directories have directories inside it.

If inside directories are also empty I can say main directory is empty otherwise it's not empty.

How can I test this?

For example:

A>A1(file1),A2 this is not empty beacuse of file1 B>B1(no file) this is empty C>C1,C2 this is empty 
like image 273
soField Avatar asked May 11 '10 12:05

soField


People also ask

What is empty directory Linux?

The command-line utility “rmdir” is used to delete empty files or directories. Rather than checking a directory whether it is empty or not, you can only delete an empty directory. In the following example, we will delete the “testfolder” directory with the help of the “rmdir” command.

What are empty directories?

An empty directory contains no files nor subdirectories. With Unix or Windows systems, every directory contains an entry for “ . ” and almost every directory contains “ .. ” (except for a root directory); an empty directory contains no other entries.

Does Git track empty directory?

To get Git to recognize an empty directory, the unwritten rule is to put a file named . gitkeep in it. Git will see the . gitkeep file in the otherwise empty folder and make that folder part of the next commit or push.


1 Answers

It depends a little on what you want to do with the empty directories. I use the command below when I wish to delete all empty directories within a tree, say test directory.

find test -depth -empty -delete 

One thing to notice about the command above is that it will also remove empty files, so use the -type d option to avoid that.

find test -depth -type d -empty -delete 

Drop -delete to see the files and directories matched.

If your definition of an empty directory tree is that it contains no files then you be able to stick something together based on whether find test -type f returns anything.

find is a great utility, and RTFM early and often to really understand how much it can do :-)

like image 69
Martin Avatar answered Oct 04 '22 13:10

Martin