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

How do I check whether a directory is empty or not under Linux / UNIX using a TCSH / C shell script?

You can use the following simple code to check if a directory is empty or not using csh:

set dir="$1"
set c=0
# make sure $dir exits 
if ( -d ${dir} ) then
    set c=`ls -a ${dir} | wc | awk '{print $1}'`
   # IS dir is empty
    if ( "${c}" == 2 ) then 
		echo "Empty directory - "${dir}
    else 	#dir has files
		echo "Dir has files - "${dir}
      echo "Error: Not a directory"

You can use the find command (see your local find man page for exact syntax) as follows:

find "/path/to/dir" -type f -exec echo Found file {} ;


find -type d -empty

See also:

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

How to Make Website WCAG Compliant?

Next Post

Link download Kali Linux 2020.1 (ISO + Torrent)

Related Posts