1

I have a directory named STA. Within that directory are about 600 other directories that have the format hh:mm:ss (for example 00:01:34). Within each of these sub-directories should be three files. I also have a file, 'waveformlist', (contained within STA) which is a list of all of these sub-directories i.e.:

00:01:34
00:02:35
etc. 

A lot of the sub-directories do not contain these three files and are instead empty. I want to run a C-shell script to go through every sub directory and check if it is empty. If it is empty I want to delete that sub directory from the main directory STA, and also remove that sub-directory name from the list 'waveformlist'.

Below is my script so far. It does not recognize when the sub-directory is empty or not and does not like the rm $dir line. Also, I do not know how to go and remove the sub-directory name from 'waveformlist'.

#!/bin/csh

echo "Enter name of station folder to apply filter to as 'STA' e.g. APZ:"
set ans = $<
cd $ans

set c=0

foreach dir (*:*)
    if ("${c}" == 0) then
    echo "Empty directory:" $dir
    rm $dir
    else
    echo ${dir} "has files"
    endif
end

I hope I have been clear enough.

4

1 回答 1

0

通常我只使用 rmdir * 因为它只会删除空目录,并且会忽略包含文件的目录和普通文件。如果你给它一个包含文件或普通文件的目录,你可能会遇到一些错误。所以这是一个非常安全的执行命令。

话虽如此,这样的事情可能对你有用

foreach d ( $ans/* )
    if -d $d then
#        echo $d is a directory                                                                                                                                         
        set numfiles = ( `ls $d| wc -l` )
        if ( $numfiles == 0 ) then
            echo rmdir $d
            rmdir $d
        endif
    endif
end
于 2014-10-18T14:21:26.550 回答