`ls $dir|wc -l` doesn't always work. A straight "ls" won't catch the existence of "hidden" files (i.e., files whose name begins with "."). You could have megabytes worth of such files and STILL think the directory was empty. You have to use the -a option to see these files.