I need a command to get a list of all folders owned by a specific user.
Is there a way to get them sorted by size and in addition with the total sum of all sizes?
My recent try was:
#!/bin/bash
read -p "Enter user: " username
for folder in $(find /path/ -maxdepth 4 -user $username) ; do
du -sh $folder | sort -hr
done
Thanks in advance!
CodePudding user response:
Try this:
#!/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"
- The
find
with-print0
is explained here: https://mywiki.wooledge.org/BashFAQ/001 - Since you want the size of each directory, you have to store all the results in a file, then sort it.