TCSH/CSH/C Shell检查目录是否为空

时间:2020-01-09 10:46:00  来源:igfitidea点击:

如何使用TCSH/C Shell脚本在Linux/UNIX下检查目录是否为空?
您可以使用以下简单代码检查目录是否为空或者不使用csh:

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

您可以按以下方式使用find命令(有关确切语法,请参见本地find手册页):

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

或者

find -type d -empty