使用shell脚本计算文件和目录
我正在学习bash脚本,并编写了一个脚本来计算作为参数提供的目录中的文件和目录。 我的工作方式对我来说似乎很奇怪,我想知道是否有一种更简单的方法。
我已经注释了可以工作的代码,但将其留作比较。 我试图让for -loop工作,而不是使用if语句来检测给定位置中的项目是文件还是目录。
编辑 :我刚刚发现,评论代码计算给定位置的子目录中的所有文件和目录! 有没有什么办法来防止这种情况,只计算给定位置的文件和目录?
#!/bin/bash
LOCATION=$1
FILECOUNT=0
DIRCOUNT=0
if [ "$#" -lt "1" ]
then
echo "Usage: ./test2.sh <directory>"
exit 0
fi
#DIRS=$(find $LOCATION -type d)
#FILES=$(find $LOCATION -type f)
#for d in $DIRS
#do
# DIRCOUNT=$[$DIRCOUNT+1]
#done
#for f in $FILES
#do
# FILECOUNT=$[$FILECOUNT+1]
#done
for item in $LOCATION
do
if [ -f "$item" ]
then
FILECOUNT=$[$FILECOUNT+1]
elif [ -d "$item" ]
then
DIRCOUNT=$[$DIRCOUNT+1]
fi
done
echo "File count: " $FILECOUNT
echo "Directory count: " $DIRCOUNT
出于某种原因, for -loop的输出,无论我指向哪个位置,总是返回:
File count: 0 , Directory count: 1
你不会迭代给定目录内的文件列表; 在$LOCATION后添加/* 。 你的脚本应该如下所示:
...
for item in $LOCATION/*
do
...
正如dogbane所指出的那样,只是添加/*将只计算不以开头的文件. ; 为此,您应该做以下事情:
...
for item in $LOCATION/* $LOCATION/.*
do
...
使用find如下所示。 该解决方案将正确计算文件名的空格,换行符和点文件。
FILECOUNT="$(find . -type f -maxdepth 1 -printf x | wc -c)"
DIRCOUNT="$(find . -type d -maxdepth 1 -printf x | wc -c)"
请注意, DIRCOUNT包含当前目录( . )。 如果你不想要这个,减1。
((DIRCOUNT--)) # to exclude the current directory
要解决您可以使用的问题:
FILECOUNT=$(find $LOCATION -type f | wc -l)
DIRCOUNT=$(find $LOCATION -type d | wc -l)
find将在$LOCATION下递归查找所有文件( -type f )或目录( -type d ); wc -l会计算每种情况下写入stdout的行数。
但是,如果你想学习,bash脚本可能是一个更好的方法。 一些评论:
$LOCATION查找文件/目录(不是递归地在其子目录下),则可以使用for item in $LOCATION/* ,其中*将展开到$LOCATION目录中的文件/目录列表。 缺少*是为什么您的原始脚本返回0/1(因为$LOCATION目录本身是唯一计数的项目)。 $LOCATION实际上是一个带有[ -d $LOCATION ]的目录。 $(( ... )) ,例如FILECOUNT=$(( FILECOUNT + 1 )) 。 find与循环结合起来。 例:
find $LOCATION | while read item; do
# use $item here...
done
链接地址: http://www.djcxy.com/p/9811.html
上一篇: Count files and directories using shell script
下一篇: How to find the largest file in a directory and its subdirectories?
