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

Re: Check if dir is empty



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.

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.

regards,

-- 
Kevin B. McCarty <kmccarty@princeton.edu>   Physics Department
WWW: http://www.princeton.edu/~kmccarty/    Princeton University
GPG public key ID: 4F83C751                 Princeton, NJ 08544



Reply to: