Reputation: 43
Given the script below
#/bin/sh
echo
echo treesize of `pwd` at `date +"%Y-%m-%d %H:%M:%S"`
echo
du -k --max-depth=1 | sort -nr | awk '
BEGIN {
split("KB,MB,GB,TB", Units, ",");
}
{
u = 1;
while ($1 >= 1024) {
$1 = $1 / 1024;
u += 1
}
$1 = sprintf("% *d%.1f %s", (7-length(sprintf("%.1f", $1))), 0, $1, Units[u]);
print $0;
}
'
echo
Getting the following output:
treesize of /home/jonathan at 2020-04-08 10:18:50
0295.4 GB .
0175.6 GB ./Documents
0118.5 GB ./vmware
047.9 MB ./Desktop
032.0 KB ./Pictures
012.0 KB ./Videos
04.0 KB ./.Public
How do I get to remove the leading 0 and have spaces instead please? Enjoy the script! Credits to someone on the internet :)
Upvotes: 0
Views: 489
Reputation: 43
Managed to find the issue: replace 0 in the sprintf command with a " ".
#/bin/sh
echo
echo treesize of `pwd` at `date +"%Y-%m-%d %H:%M:%S"`
echo
du -k --max-depth=1 | sort -nr | awk '
BEGIN {
split("KB,MB,GB,TB", Units, ",");
}
{
u = 1;
while ($1 >= 1024) {
$1 = $1 / 1024;
u += 1
}
$1 = sprintf("%8.1f %s", $1, Units[u]);
print $0;
}'
echo
And now displays :
treesize of /home/jonathan at 2020-04-08 11:03:29
295.5 GB .
175.6 GB ./Documents
118.5 GB ./vmware
1003.7 MB ./.cache
268.1 MB ./.config
47.9 MB ./Desktop
32.0 KB ./Pictures
12.0 KB ./Videos
4.0 KB ./.Templates
4.0 KB ./.Public
4.0 KB ./Music
4.0 KB ./Downloads
Enjoy!
Upvotes: 0
Reputation: 2715
Use sed
sed 's#^[[:space:]]*0# #g'
^
-- start of the line
[[:space:]]
<- Space character
*
<-- 0 or more occurence
#/bin/sh
echo
echo treesize of `pwd` at `date +"%Y-%m-%d %H:%M:%S"`
echo
du -k --max-depth=1 | sort -nr | awk '
BEGIN {
split("KB,MB,GB,TB", Units, ",");
}
{
u = 1;
while ($1 >= 1024) {
$1 = $1 / 1024;
u += 1
}
$1 = sprintf("% *d%.1f %s", (7-length(sprintf("%.1f", $1))), 0, $1, Units[u]);
print $0;
}' | sed 's#^[[:space:]]*0# #g'
echo
Upvotes: 1