# !/bin/bash
read -rp "Enter user: " username
tmp_du=$(mktemp)
path="/path"
# Get a list of directories owned by "$username", and their size, store it in a file
find "$path" -maxdepth 4 -type d -user "$username" -exec du -s {} \; 2>/dev/null | sort -n >"$tmp_du"
# Add the first column of that file
sum=$( awk '{ sum+=$1 } END { print sum; }' "$tmp_du" )
# Output, must output first column in human readable format
numfmt --field=1 --to=iec <"$tmp_du"
# Output total
echo "Total: $(echo "$sum" | numfmt --to=iec)"
rm -f "$tmp_du"
1条答案
按热度按时间whhtz7ly1#
试试这个(版本1,不完整,请参阅下面的版本2!)
find
和-print0
的解释如下:https://mywiki.wooledge.org/BashFAQ/001版本2包括我在第一个答案中忘记的-USER,加上仅考虑该用户的目录的总数: