Good day, everyone!
I'm very new to bash scripting. Our teacher gave us a task to create a script that basically does the same job the 'du' command does, with the difference that 'du' command gives an output in the form of
<size> <folder name>
and what we need is
<folder name> <size>
As for the moment, I came up with the following script:
du $1 2> /dev/null |\
sort -nr -k1 |\
head -10 |\ #We need only to show 10 which are the biggest ones
cut -f2 |\
while read fname
do
printf "${fname}\t`du -h --summarize \"$fname\" 2> /dev/null | cut -f1`\n"
done
Generally it does what it should, but is very-very slow. For example, on my '/usr' folder 'du' command (with everything before 'while' included) works for about 2 minutes and 30 seconds, while the script runs for 7 minutes and 20-30 seconds.
Is there a way to make it work faster? As I wrote before, I'm very new to scripting, so no doubt I'm missing something important.
Thank you for all your help in advance!