Re: Empty Directory problem

From: Scott McMillan (smcm_at_usa.net)
Date: 10/27/03


Date: Mon, 27 Oct 2003 16:09:41 -0500

On 27 Oct 2003 10:55:02 -0800, anthonypieper@cs.com (newexpectuser)
wrote:

>I have a script running and I want to see if a directory is empty
>(meaning not having any files).
>
>I can't use the rmdir, because I don't want to remove the directory, I
>just want to do the following:
>
>
>if [ $DIR is empty ]
> echo "The directory is empty" > $LOG/logfile.dat
> exit
>fi
>
>Thank you

I wonder if this would work for you (filenames starting with a period
would be found as well, subdirectories not checked):

cd /<yourdirectoryname>
if [ -z `find ./ -type f -print` ]; then
   echo "Directory is empty"
else
   echo "Directory contains files"
fi

If you are not concerned with whether or not 'hidden' (filenames
beginning with a period) are in the directory, I think you could use
something like:

cd /yourdirectoryname
ls -l | grep "total 0" >/dev/null
if [ $? = 0 ]; then
   echo "Directory is empty"
else
   echo "Directory contains files"
fi

However, subdirectories within <yourdirectoryname> would trigger
"Directory contains files".

Scott McMillan



Relevant Pages

  • filtype() Problems
    ... I wrote a simple script utilizing readdirto read the contents of a directory, then of all subdirectories and thier contents and echo the filenames to the page. ...
    (comp.lang.php)
  • Wordpress <=2.0.2 cache shell injection
    ... if user registration is enabled, ... php files. ... DB_PASSWORD costant could be empty, ... echo 'No response from '.$host.':'.$port; die; ...
    (Bugtraq)
  • Re: Bash problems?
    ... The first returns true for either empty files or empty directories; the second returns true only if the given file is an empty directory. ... $ if isempty emptydir; then echo 'yes'; else echo 'no'; fi ... $ if isempty emptyfile; then echo 'yes'; else echo 'no'; fi ...
    (Fedora)
  • Re: what triggers "you have mail" (OFFLIST)
    ... echo $MAIL ... when $MAIL is empty. ... that I am getting incoming mail in /usr/david/tmp which each ... tag. ...
    (freebsd-questions)
  • Re: Testing if an array is empty
    ... A function reads a mysql record into an array and returns it, ... What ways do I have to test that my returned array is not empty ... if) echo '$array is empty'; ...
    (comp.lang.php)