Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 计算每个文件夹的平均文件数_Linux_Bash_Shell_Shebang - Fatal编程技术网

Linux 计算每个文件夹的平均文件数

Linux 计算每个文件夹的平均文件数,linux,bash,shell,shebang,Linux,Bash,Shell,Shebang,因此,我正在编写一个shell脚本,它将计算用户选择的文件夹中的平均文件量 我只是不知道该怎么办 首先,我们要设置用户文件夹选择 #!/bin/sh # Script for folder info #------------------------------------------- WANTEDFOLDER=$PWD cd cd $1 #Go into the folder (first argument) $WANTEDFOLDER/folderinfo.sh # -----------

因此,我正在编写一个shell脚本,它将计算用户选择的文件夹中的平均文件量

我只是不知道该怎么办

首先,我们要设置用户文件夹选择

#!/bin/sh
# Script for folder info
#-------------------------------------------
WANTEDFOLDER=$PWD
cd
cd $1 #Go into the folder (first argument)
$WANTEDFOLDER/folderinfo.sh
# ------------------------------------------
echo "Selected directory: $PWD"
我又添加了几个类似的命令,但这些命令正在工作,所以为了可读性,我不在这里添加它们

所以我想知道,我怎么可能计算出位于指定目录的文件夹中的平均文件数

为了更好地理解这一点,让我们建立一个示例目录

$ mkdir testdir
$ cd testdir
$ mkdir 1
$ mkdir 2
$ mkdir 3
$ cd 1
$ echo "hello" > wow.txt
$ cd ..
$ cd 2
$ echo "World"; > file.c
$ echo "file number 2" > twoinone.tex
$ cd ..
$ cd 3
$ echo "zzz" > z.txt
$ echo "zz2" > z2.txt
$ echo "zz3" ? z3.txt
好的,为了更好的说明,在这之后,文件夹看起来像这样

所以首先,我们要做
find-f类

这将打印以下结果

/1/wow.txt
/2/file.c
/2/twoinone.tex
/3/z.txt
/3/z2.txt
/3/z3.txt

现在我们在目录1中有一个文件,在2中有2个,在3中有3个。 所以要计算平均值,它是(1+2+3)/3,结果是2

这也可以写进一个

(所有唯一文件的数量)/(所有唯一目录的数量)

一件事是在你的头脑中计算,另一件事是在实际的shell脚本中计算

换句话说,我需要以某种方式转换成脚本,它将计算特定文件夹中的文件数量,存储它,将所有文件一起计算,然后最终将其除以唯一目录的数量

因此,要获得目录的数量,我们必须执行
find-d型| wc-l

要获取文件量,非常简单。我们将使用
查找-f型| wc-l

但我不知道的是,我如何将这两个值存储在一个变量中,然后将它们彼此分开

此外,理想情况下,我更喜欢以特定行的方式格式化代码

例如,
echo“平均文件数:$(find.-type f | wc-l(以某种方式划分)find.-type d | wc-l)”


你知道如何完成这个壮举吗

也许这就是你想要的:

echo $(( $(find . -type f | wc -l) / $(find . -type d | wc -l) ))

也许这就是你想要的:

echo $(( $(find . -type f | wc -l) / $(find . -type d | wc -l) ))
1.)是否确实需要获得所有目录中平均文件数的浮点估计值?但你提出了这个问题,所以至少目前需要解决这个问题

#!/bin/bash

NumFiles=0

#Loop over all directories in the current location and count the files in each directory
DirCount=0
for d in */
do
        FilesInDir=$(\ls -A $d|wc -l)
        echo "Number of files in directory $d is $FilesInDir"
        NumFiles=$(($NumFiles+$FilesInDir))
        DirCount=$(($DirCount + 1))
done

echo Final number of directories: $DirCount
echo Total files in directories: $NumFiles

if [ $DirCount -gt 0 ]; then
        AvgFiles=$(echo "$NumFiles/$DirCount" | bc -l)
        echo $AvgFiles
fi
1.)是否确实需要获得所有目录中平均文件数的浮点估计值?但你提出了这个问题,所以至少目前需要解决这个问题

#!/bin/bash

NumFiles=0

#Loop over all directories in the current location and count the files in each directory
DirCount=0
for d in */
do
        FilesInDir=$(\ls -A $d|wc -l)
        echo "Number of files in directory $d is $FilesInDir"
        NumFiles=$(($NumFiles+$FilesInDir))
        DirCount=$(($DirCount + 1))
done

echo Final number of directories: $DirCount
echo Total files in directories: $NumFiles

if [ $DirCount -gt 0 ]; then
        AvgFiles=$(echo "$NumFiles/$DirCount" | bc -l)
        echo $AvgFiles
fi

谢谢,简单有效的解决方案。我没有意识到我可以简单地用算术表达式来表示两条不同的命令,这是允许你做算术运算的
$(..)
。请注意,这只是一个整数运算,所以如果你在2个目录中有5个文件,它会告诉你2不是2.5的平均值。非常正确。如果您希望得到非整数结果,您可以使用
dc
awk
进行计算。谢谢,简单有效的解决方案。我没有意识到我可以简单地用算术表达式来表示两条不同的命令,这是允许你做算术运算的
$(..)
。请注意,这只是一个整数运算,所以如果你在2个目录中有5个文件,它会告诉你2不是2.5的平均值。非常正确。如果希望得到非整数结果,可以使用
dc
awk
进行计算。