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

Re: Check if dir is empty



On Fri, Jan 21, 2005 at 09:15:26AM -0500, Kevin B. McCarty wrote:
> if [ -n "$(ls -A "$directory")" ] ; then
> 	# do something
> fi
> 
> n.b. I suspect "ls -A" is a GNU extension, so probably is not portable.
>  Also the test will give a false negative if the directory does not have
> read permission.

When I investigated this I saw this, and I think this gets around such 
problems:

if [[ `ls "$directory" 2>/dev/null | wc -l` -ne 0 ]]; then
  #do something
fi



Reply to: