LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to display directory size? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-display-directory-size-938944/)

sam_nyc 04-09-2012 02:16 PM

How to display directory size?
 
Hi,

I have many directories under /home. What is the fastest way to find who is taking lot of space.

So for this is what I did.

Code:

cd /home
du -sh * |more

The above command takes long time to run. Is there a different command?
My goal is, I want to send email each month how much space is under their directory.

Thanks.

Kustom42 04-09-2012 03:00 PM

There is no faster alternative to the du command. To accomplish your task it would be advised to create a shell script that does a du of each home directory independently, du does not take much resources and will complete faster if you run a seperate process for each directory. You can then do a test with an if statement or a similar function and email out any directories that are over a certain size.

Satyaveer Arya 04-09-2012 03:11 PM

You can also check with the help of Disk Usage Analyzer. In Gnome it is under Applications --> Accessories --> Disk Usage Analyzer.

Kustom42 04-09-2012 03:41 PM

Quote:

Originally Posted by Satyaveer Arya (Post 4648656)
You can also check with the help of Disk Usage Analyzer. In Gnome it is under Applications --> Accessories --> Disk Usage Analyzer.

That is simply a graphical interface that runs the du command for you. It is more resource intensive(barely) as it uses the x windows system but sam_nyc did state he wants a monthly print out and email automatically.


Try a basic script such as below set up on a monthly cron job and modify it for your needs.

This is a simply for loop but you can make it a better script by doing it in a few different ways, I'm just a bit lazy right now.

Code:

#!/bin/bash
for homedir in $(ls -d /home/*); do du -sh $homedir >> /tmp/$(date +%m-%d-%Y)-home-directory-sizes.txt; done
EMAILMESSAGE=/tmp/$(date +%m-%d-%Y)-home-directory-sizes.txt
SUBJECT=$(date +%m-%d-%Y) Home Directory Usage
SENDTO=youremail@domain.com
/bin/mail -s "$SUBJECT" "$SENDTO" < $EMAILMESSAGE

Set it up as a cron job around 11 P.M. and redirect the output to /dev/null or a text file for logging if you need it. Do not set this up to run at midnight as the date will be different once the du's complete and cause the email to not send correctly.

Satyaveer Arya 04-09-2012 03:50 PM

You can also use this code written in perl:

Quote:

#!/usr/bin/perl
use strict;
use warnings;
use Filesys:: DiskSpace;
my $dir = "/home/*";
my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
my $df_free = (($avail) / ($avail+$used)) * 100.0;
my $out = sprintf("Disk space on $dir: %0.2f\n",$df_free);
print $out;

Kustom42 04-09-2012 04:42 PM

I use python over perl but still a decent script if you want to go that route. However, you would want to use du instead of df as it is more accurate and in some cases df can provide very inaccurate results. I've run into several issues where syslog processes where causing df to read higher disk usage by almost 20 GB even though the files it had open were no longer that size.

vp0619520 04-10-2012 05:17 AM

Hi,you can use this command
Code:

du -h --max-depth=1 /home/|sort -hr
:)

sam_nyc 04-10-2012 11:00 AM

Thank you so much for your all help. The script really helps. I will add this to my cronjob.


All times are GMT -5. The time now is 07:33 AM.