我需要一個命令來獲取特定用戶擁有的所有檔案夾的串列。
有沒有辦法讓它們按大小排序,再加上所有大小的總和?
我最近的嘗試是:
#!/bin/bash
read -p "Enter user: " username
for folder in $(find /path/ -maxdepth 4 -user $username) ; do
du -sh $folder | sort -hr
done
提前致謝!
uj5u.com熱心網友回復:
試試這個(第 1 版,不完整,請參閱下面的第 2 版!)
#!/bin/bash
tmp_du=$(mktemp)
path="/path"
find "$path" -maxdepth 4 -type d -print0 | while IFS= read -r -d '' directory
do
du -sh "$directory" >>"$tmp_du"
done
sort -hr "$tmp_du"
echo ""
echo "TOTAL"
du -sh "$path"
rm -f "$tmp_du"
- 在這里解釋:https
find: //mywiki.wooledge.org/BashFAQ/001-print0 - 由于您想要每個目錄的大小,因此您必須將所有結果存盤在一個檔案中,然后對其進行排序。
帶有我在第一個答案中忘記的 -user 的版本 2,加上僅考慮該用戶的目錄的總數:
#!/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"
- 這里所有的目錄大小都存盤在一個檔案中
- 總計是第一列的總和
- 要以類似于 du 的 -h 的格式輸出數字,請使用 numfmt。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/517846.html
標籤:Unix命令
上一篇:如何使用bash或Perl將特定檔案名放入特定的JSON格式?
下一篇:bash中的三元運算或默認值
