我需要循环编写脚本,该脚本将计算文件和目录的数量,并指示哪个刨丝器和数量。像其他这样:文件比目录多10个。我正在尝试类似的操作,但是它只显示文件和目录,我不知道如何指示更大的内容等。感谢您的任何帮助
shopt -s dotglob
count=0
for dir in *; do
test -d "$dir" || continue
test . = "$dir" && continue
test .. = "$dir" && continue
((count++))
done
echo $count
for -f in *; do
"$fname"
done
这是我前一段时间使用的递归目录遍历。添加了对目录和文件的计数:
#!/bin/sh
# recursive directory walk
loop() {
for i in *
do
if [ -d "$i" ]
then
dir=$((dir+1))
cd "$i"
loop
else
file=$((file+1))
fi
done
cd ..
}
loop
echo dirs: $dir, files: $file
将其粘贴到script.sh
并运行:
$ sh script.sh
dirs: 1, files: 11
您可以使用find命令使事情变得更简单。
以下命令将列出给定路径中的所有文件:
find "path" -mindepth 1 -maxdepth 1 -type f
并且还使用-type d
您将获得目录。
在wc -l
中查找管道将为您提供编号,而不是实际的文件和目录名称,因此:
root="${1:-.}"
files=$( find "$root" -mindepth 1 -maxdepth 1 -type f | wc -l)
dirs=$( find "$root" -mindepth 1 -maxdepth 1 -type d | wc -l)
if [ $files -gt $dirs ]; then
echo "there are $((files - dirs)) more files"
elif [ $files -lt $dirs ]; then
echo "there are $((dirs - files)) more dirs"
else
echo "there are the same"
fi