I would like to know how I can get the total size in bytes and then convert it to a human readable measure (kb, mg, gb) of all files for a specific user. I am testing this:
ALL_FILES=$(find / -user $NAME -type f 2>>./.tmp.txt)
TOTAL_SIZE=$(echo $ALL_FILES | du -s | cut -f 1)
It is clear that this does not work even remotely, it does not matter what user passes to $NAME, it always returns the same value.
Thanks.
Try the following:
du -s is calculating the space used in the current directory, not the list of files.
Try with
awk
.What it does
-printf "%s\n"
is print only the size of the file in bytes followed by a line break (same as we will use for the next command).Next, we will use
awk
to accumulate the sizes of the files obtained byfind
, in the variabletotal_size
. At the end, we will print it.