命令脚本将文件识别为目录

Command script recognizes files as directories

下面的代码应该计算一个目录包含的元素的数量,但在正确执行的同时,它还将当前目录中的每个元素识别为一个目录 .

我不知道如何不显示不是目录的元素。我该怎么做?

Code is here: http://pastebin.com/9R4eB4Xn

termlog.txt: https://justpaste.it/tgsl

如您所见,.jpg 或 .zip 等文件被识别为目录。

您的 echo "Element is a directory"ifthen 之间。将它移到 then 之后:

for i in *
do
  if [ ! -f "$i" ] && [ -d "$i" ]
    then
      echo "Element is a directory"
      FILES=`ls -l "$i" | wc -l`  # List the content of "$i" directory
                                  # and count the number of lines
      FILES2=`expr $FILES - 1`    # Substract one because one line is
                                  # occupied with the number of blocks
      echo "$i: $FILES2"          # Shows the name of the directory and
                                  # the number of inputs that it has
  fi
done
for i in `find DIRECTORY -maxdepth 2 -type d`; do echo "$i: `ls -1 $i | wc -l`"; done

如果只对当前目录感兴趣,请将 DIRECTORY 替换为 .