Home > OS >  Sorted List of folders (by size) of an specific user
Sorted List of folders (by size) of an specific user

Time:10-16

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"
  • Related