TCSH / CSH / C Shell Check Whether a Directory is Empty or Not

https‮/:‬/www.lautturi.com
TCSH / CSH / C Shell Check Whether a Directory is Empty or Not

To check whether a directory is empty or not in the tcsh, csh, or c shell, you can use the test command with the -d and -z options.

For example, to check whether the directory /path/to/dir is empty, you can use the following command:

if ( -d /path/to/dir && ! -z /path/to/dir ) then
  echo "Directory is not empty"
else
  echo "Directory is empty"
endif

This will test whether /path/to/dir is a directory (-d /path/to/dir) and whether it is empty (! -z /path/to/dir). If both conditions are true, the script will print "Directory is not empty", otherwise it will print "Directory is empty".

The -d option tests whether the specified file is a directory, and the -z option tests whether the file is empty. The ! operator negates the test, so ! -z tests whether the file is not empty.

You can also use the test command with the -e option to test whether the specified file or directory exists.

For example:

if ( -e /path/to/dir ) then
  echo "Directory exists"
else
  echo "Directory does not exist"
endif

This will test whether the directory /path/to/dir exists, and print "Directory exists" if it does or "Directory does not exist" if it does not.

Keep in mind that the test command is built into the tcsh, csh, and c shells, so you do not need to install it separately.

Created Time:2017-10-30 14:27:21  Author:lautturi