开发者

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 u开发者_开发知识库se Unix find to do it?

I tried

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

Doesn't seem to work.


Does your find have predicate -empty?

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


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.


-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?


You can also use:

find . -type d -links 2

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜