LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-25-2008, 04:56 PM   #1
abolishtheun
Member
 
Registered: Mar 2008
Posts: 183

Rep: Reputation: 31
listing file size by percentage?


is there a program like du(1) but can list the sizes in terms of the percentage of the total size in a directory? imagine something like this:

Code:
% foo ~/dir-to-measure

13.00 /home/user/dir-to-measure/subdirA
20.00 /home/user/dir-to-measure/subdirB
30.00 /home/user/dir-to-measure/subdirC
100.00 /home/user/dir-to-measure
this would tell you that subdirA contains 13% of the total of ~/dir-to-measure. I occasionally need something like this to find out what's taking up so much space on a disk. Any help?
 
Old 03-25-2008, 05:26 PM   #2
BlueC
Member
 
Registered: Aug 2007
Posts: 122

Rep: Reputation: 17
you could try "baobab" which is part of gnome-utils package - its a GUI utility and does exactly what you want, and more.

i'm pretty sure that feeding the right options to du can do what you want too... just not sure how
 
Old 03-25-2008, 06:18 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Or you can try to write a simple script which performs the calculation, e.g. something like
Code:
#!/bin/bash
total=$(du -s $1 | gawk '{print $1}')
du --max-depth=1 $1 | while read line
do
   size=$(echo $line | gawk '{print $1}')
   dir=$(echo $line | gawk '{print $2}')
   percent=$(echo "scale=4; $size / $total * 100" | bc)
   printf "%6.2f%%   %s\n" $percent $dir
done
the output will be something like
Code:
$ foo ~/doc/
 44.78%   /home/colucix/doc/shell
  0.08%   /home/colucix/doc/help
  2.04%   /home/colucix/doc/articles
 51.97%   /home/colucix/doc/manual
100.00%   /home/colucix/doc/
 
Old 03-25-2008, 08:19 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Nice :}

A little suggestion:
Code:
du --max-depth=1 $1 | sort -g | while read line

Cheers,
Tink
 
Old 03-25-2008, 09:17 PM   #5
abolishtheun
Member
 
Registered: Mar 2008
Posts: 183

Original Poster
Rep: Reputation: 31
That looks very nice! The only down side I see is that it calculates the size of every file twice, doesn't it?
 
Old 03-25-2008, 10:30 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
no it doesn't
 
Old 03-25-2008, 11:24 PM   #7
abolishtheun
Member
 
Registered: Mar 2008
Posts: 183

Original Poster
Rep: Reputation: 31
does it not calculate the total size, then again in the while loop?
 
Old 03-26-2008, 12:52 AM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
nope :}

Show me which line you think does that?


Cheers,
Tink
 
Old 03-26-2008, 01:21 AM   #9
abolishtheun
Member
 
Registered: Mar 2008
Posts: 183

Original Poster
Rep: Reputation: 31
Every file is counted once here:
Code:
#!/bin/bash
total=$(du -s $1 | gawk '{print $1}')
and then again here:
Code:
du --max-depth=1 $1 | while read line
does du cache?
 
Old 03-26-2008, 01:25 AM   #10
ak_random
Member
 
Registered: Jun 2007
Location: Silicon Valley, CA
Distribution: Xubuntu
Posts: 83

Rep: Reputation: 15
Quote:
Originally Posted by abolishtheun View Post
does du cache?
I doubt it does any caching between invocations. However, a lot of the file system metadata loaded by du should still be in the buffer cache, so depending on how many files you have under that directory, two runs of du might still execute pretty quickly.
 
Old 03-26-2008, 01:58 AM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by abolishtheun View Post
Every file is counted once here:
Code:
#!/bin/bash
total=$(du -s $1 | gawk '{print $1}')
and then again here:
Code:
du --max-depth=1 $1 | while read line
does du cache?
OK. I wouldn't have referred to a run of du as
a calculation. I suppose if you were concerned
with running du twice (and no, it doesn't cache,
but your file-system most likely does) you could
use a different approach, e.g. run du ones, stick
the results in an array with awk, add them up, and
then calculate the percentages on a second run.


Cheers,
Tink
 
Old 03-26-2008, 04:10 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Ok. Here's a solution which uses du only once, to avoid problems with the buffer cache:
Code:
#!/bin/bash
du --max-depth=1 $1 | sort -gr | while read line
do
   test -z "$total" && total=$(echo $line | cut -f 1 -d \ )
   size=$(echo $line | cut -f 1 -d \ )
   dir=$(echo $line | cut -f 2- -d \ )
   percent=$(echo "scale=4; $size / $total * 100" | bc)
   printf "%6.2f%%   %s\n" $percent "$dir"
done | tac
also introduced the sort -g command as suggested by Tinkster. Furthermore I substituted the gawk statements with cut to manage directories containing blank spaces in their name, by extracting from the second field to the end of the line:
Code:
dir=$(echo $line | cut -f 2- -d \ )
 
Old 03-26-2008, 06:22 AM   #13
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Nice script!

I added in a section to give the directory size as a second column. There's probably a more elegant way, but it seems to work:
Code:
#!/bin/bash

du --max-depth=1 $1 | sort -gr | while read line
do
   test -z "$total" && total=$(echo $line | cut -f 1 -d \ )
   size=$(echo $line | cut -f 1 -d \ )
   if [ $size -lt 1000 ]; then
      printsize="$size"K
   elif [ $size -ge 1000 ] && [ $size -lt 1000000 ]; then
      printsize=$(echo "scale=1; $size/1024" | bc)M
   elif [ $size -ge 1000000 ] && [ $size -lt 1000000000 ]; then
      printsize=$(echo "scale=1; $size/1024000" | bc)G
   fi
   dir=$(echo $line | cut -f 2- -d \ )
   percent=$(echo "scale=4; $size / $total * 100" | bc)
   printf "%6.2f%%\t%s\t%s\n" $percent $printsize "$dir"
done | tac
You can add in tera and petabytes if you need them!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Listing Directory Size andy7t Linux - Software 2 12-14-2008 02:46 PM
any ideas to reduce log file size or make log file size managed? George2 Programming 2 08-13-2006 06:55 AM
Listing program size cupoftea Linux - Newbie 4 05-16-2006 09:23 AM
directory size listing with ls mikemrh9 Linux - General 2 01-05-2005 12:04 PM
file system size larger than fysical size:superblock or partition table corrupt klizon Linux - General 0 06-18-2004 04:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration