简体   繁体   中英

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.

Does your find have predicate -empty ?

You should be able to use find . -type d -empty 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM