LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-09-2012, 02:16 PM   #1
sam_nyc
Member
 
Registered: Aug 2009
Posts: 73

Rep: Reputation: 1
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.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-09-2012, 03:00 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
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.
 
Old 04-09-2012, 03:11 PM   #3
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
You can also check with the help of Disk Usage Analyzer. In Gnome it is under Applications --> Accessories --> Disk Usage Analyzer.
 
Old 04-09-2012, 03:41 PM   #4
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Quote:
Originally Posted by Satyaveer Arya View Post
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.
 
2 members found this post helpful.
Old 04-09-2012, 03:50 PM   #5
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
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;

Last edited by Satyaveer Arya; 04-09-2012 at 03:51 PM.
 
3 members found this post helpful.
Old 04-09-2012, 04:42 PM   #6
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
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.
 
1 members found this post helpful.
Old 04-10-2012, 05:17 AM   #7
vp0619520
Member
 
Registered: Jan 2012
Posts: 55

Rep: Reputation: Disabled
Hi,you can use this command
Code:
du -h --max-depth=1 /home/|sort -hr
 
2 members found this post helpful.
Old 04-10-2012, 11:00 AM   #8
sam_nyc
Member
 
Registered: Aug 2009
Posts: 73

Original Poster
Rep: Reputation: 1
Thank you so much for your all help. The script really helps. I will add this to my cronjob.
 
  


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
Display all files below a directory path with their size from lowest to highest .... mfarber Linux - Newbie 2 01-24-2012 07:10 PM
[SOLVED] Store the size of largest file from a file/directory listing into variable SIZE lainey Linux - Newbie 3 11-15-2011 12:29 PM
Command "ls" display the directory size of ZERO cakuba Linux - Newbie 7 06-03-2010 12:01 PM
directory size includes size of .. leostar_10 Linux - Newbie 4 12-03-2007 04:12 PM
redhat command to display the size of directory please! sandrinechen Linux - Newbie 4 04-19-2004 03:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:54 AM.

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