neversaint
neversaint

Reputation: 64024

Find Directories With No Files in Unix/Linux

I have a list of directories

/home
  /dir1
  /dir2
  ...
  /dir100

Some of them have no files in it. How can I use Unix find to do it?

I tried

find . -name "*" -type d -size 0 

Doesn't seem to work.

Upvotes: 9

Views: 7744

Answers (5)

Sergi
Sergi

Reputation: 1

The answer of Pimin Konstantin Kefalou prints folders with only 2 links and other files (d, f, ...).

The easiest way I have found is:

for directory in $(find . -type d); do
   if [ -n "$(find $directory -maxdepth 1 -type f)" ]; then echo "$directory"
   fi
done

If you have name with spaces use quotes in "$directory".

You can replace . by your reference folder.

I haven't been able to do it with one find instruction.

Upvotes: 0

You can also use:

find . -type d -links 2

. and .. both count as a link, as do files.

Upvotes: 1

pixelbeat
pixelbeat

Reputation: 31728

-empty reports empty leaf dirs. If you want to find empty trees then have a look at: http://code.google.com/p/fslint/source/browse/trunk/fslint/finded

Note that script can't be used without the other support scripts, but you might want to install fslint and use it directly?

Upvotes: 1

jasonmp85
jasonmp85

Reputation: 6817

If you're a zsh user, you can always do this. If you're not, maybe this will convince you:

echo **/*(/^F)

**/* will expand to every child node of the present working directory and the () is a glob qualifier. / restricts matches to directories, and F restricts matches to non-empty ones. Negating it with ^ gives us all empty directories. See the zshexpn man page for more details.

Upvotes: 1

David M
David M

Reputation: 4376

Does your find have predicate -empty?

You should be able to use find . -type d -empty

Upvotes: 18

Related Questions