For my classes, I had to finish this task:
Directory's disk usage list
For indicated directory print a list of files and subdirectories in descending order according to their total disk usage. Disk usage of a simple file is equivalent to its size, for a subdirectory - a sum of the sizes of all files in its filesystem branch (subtree). Printed list format:
<disk usage> <holder name> <type> <name>
In the preceding print format is in case of a file not being a directory an owner, otherwise, the holder of a directory is the owner of the files having greatest total disk usage in it.<type>
is a letter: d for a directory, - for a standard file, l - for a symbolic link etc.Below file disk usage list, print a summary:
Total disk usage of the files in directory's subtree, the list of total disk usage of files in directory's subtree with respect to the owners (sums over owners). CAUTION: during recursive directories listing do not dereference symbolic links.
Here is my code:
#!/bin/bash
cd "$1"
ls -lA | awk 'NR != 1{
name=$8
for ( i=9; i<=NF; i++) name=name " " $i
type=substr($1,1,1)
if (type!="d")
{
print $5,$3,type,name
sum_all+=$5
usr_sum_all[$3]+=$5
}
else
{
sum=0;delete usr_sum;
find_proc="find " name " -printf \"%s %u\\n\" 2>/dev/null"
while ( find_proc | getline )
{
sum+=$1;usr_sum[$2]+=$1;
sum_all+=$1;usr_sum_all[$2]+=$1;
}
close(find_proc)
owner=""
owner_sum=-1
for ( iowner in usr_sum ) if ( usr_sum[iowner] > owner_sum ) {owner = iowner; owner_sum=usr_sum[iowner]}
print sum " " owner " d " name
}
}
END {
print "Space taken: " sum_all
for ( iowner in usr_sum_all ) print "\t user: " iowner " " usr_sum_all[iowner]
}' | sort -gr
Is it ok? Should I make any changes?