我正在尝试获取多个用户共享文件夹使用的总字节数。
我在一个更大的脚本中得到了这个:
cd /home/user1/SharedFolder/
echo "Total for user1 : "
ls -lrt | awk '{ Total1 += $5 }; END { print Total1 " bytes"}'
cd /home/user2/SharedFolder/
echo "Total for user2 : "
ls -lrt | awk '{ Total2 += $5 }; END { print Total2 " bytes"}'
这让我可以单独查看每个文件夹中可用的字节数。我不确定这是否是获得我想要的结果的正确方法。
我遇到的问题是从包含该文件夹的所有用户获取总数量(用户数量可能会有所不同)
我刚开始使用 Linux,如有任何帮助,我们将不胜感激。谢谢。
最佳答案
第一课是使用正确的工具来完成工作:要计算目录中所有文件的大小,请使用 du
.另外,don't parse ls
.
# variable "homeDirs" will be an associative array
declare -A homeDirs
# read the /etc/passwd file, and map each user to the home directory
while IFS=: read -ra entry; do
homeDirs["${entry[0]}"]="${entry[5]}"
done < /etc/passwd
# loop over all the users
for user in "${!homeDirs[@]}"; do
dir="${homeDirs[$user]}/SharedFolder"
# if that user has a shared folder
if [[ -d "$dir" ]]; then
# find the total size
totalSize=$(du -sb "$dir" | awk '{print $1}')
# and output the information
printf "Total for user %s: %d\n" "$user" "$totalSize"
fi
done
关于linux - 多个用户共享目录的总使用量 - LINUX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42612624/