[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Check if dir is empty



On Friday 21 Jan 2005 10:37, Jacob Friis Larsen wrote:
> > > How can I check if a directory is empty?
> >
> > use the "ls" command on the dir "man ls"
>
> Yes, but I need a script to act if there are files in a directory.

I started a discussion of your problem on IRC after coming up with a comedy 
answer. This lead to a discussion of sensible ways to do it. Ypu will have 
to make these work, but they are a list of pointers. 

0) (comedy answer)

rmdir $dir && makedir $dir && dosomething

if $dir is non-empty it fails there. This might be wrong so be careful.

Two on the theme of "finding" nonexistent files inside the dir
1)  find dir -mindepth 1
2) `updatedb && locate dir | wc -l` = "1"

Hope this helps

Pete



Reply to: