LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-22-2003, 02:03 PM   #1
cli_man
Member
 
Registered: Apr 2002
Location: New York, USA
Distribution: Redhat 7.2, 9.0 Slackware 9.1
Posts: 428

Rep: Reputation: 30
Check disk space used in a directory


I am looking for a script that I can put in cron to run daily and check all the directorys directly off the /home directory and then email me any of them that are over 500 Megs in size. I can't find anything that will do that so I thought someone here would probably know.

What I need this for: I have servers that I do webhosting on, I want a report of all the users that are over there 500 meg limit, the script has to check all and add up the amount of data in the directory and subdirecties of each website.

If you know of anything like that please help me out, I could really use this. Thanks.
 
Old 08-22-2003, 03:03 PM   #2
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
Sounds like a good time to learn bash scripting.

"du -hs /home/*" as root will give you the size of each directory in /home.

"du -hs /home/* | sort -r" will sort them from largest to smallest.

"du -hs /home/* | sort -r > /root/home_report" will put them into a file you can mail to yourself.

"mail" is the program you will use to send yourself the report. Haven't used it, so I can't really help you there.

You can put it all into a couple-line bash script, and put that into your cron.

Or... Set-up disk quotas...

https://www.redhat.com/docs/manuals/...sk-quotas.html
 
Old 08-22-2003, 03:10 PM   #3
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Here is some thing simple that should work:
Code:
#!/bin/bash
IFS="
"
for dir in `ls /home`; do
out=`du -s /home/$dir`
tot=`echo $out | awk {'print $1'}`
if [ $tot -gt 512000 ];then
echo "Dir "`echo $out | awk {'print $2'}`" is over 500MB"
fi
done
 
Old 08-22-2003, 03:59 PM   #4
cli_man
Member
 
Registered: Apr 2002
Location: New York, USA
Distribution: Redhat 7.2, 9.0 Slackware 9.1
Posts: 428

Original Poster
Rep: Reputation: 30
Dang your good, thanks, and yes I know I need to learn some bash scripting I just haven't had the time yet. That script does exactly what I needed.
 
Old 02-06-2009, 11:39 AM   #5
boothcat4320
LQ Newbie
 
Registered: Feb 2009
Posts: 2

Rep: Reputation: 0
Addition

Quote:
Originally Posted by ranger_nemo View Post
Sounds like a good time to learn bash scripting.

"du -hs /home/*" as root will give you the size of each directory in /home.

"du -hs /home/* | sort -r" will sort them from largest to smallest.

"du -hs /home/* | sort -r > /root/home_report" will put them into a file you can mail to yourself.

"mail" is the program you will use to send yourself the report. Haven't used it, so I can't really help you there.

You can put it all into a couple-line bash script, and put that into your cron.

Or... Set-up disk quotas...
I have a small correction.

"sort -r" will not accomplish what you want to to for 2 reasons.

1) "sort -r" only reverses the sort order, if the "du" command sends it out of order, the "sort -r" will not fix it only reverse it.

2) "du -hs" will put the results in a human readable form. This means that 281K will be considered a higher number that 142G by the "sort" command.

A better, albeit less readable, solution would be:
"du -s /home/* | sort -nr"

As for mailing the results, you can use the following command to email the results:
"du -s /home/* | sort -nr | mail -s 'Home Directory Usage' email@domain.com"

I use the following crontab command to monitor my /var directory (Website and log files can get out of control). It mails me the results every Sunday night.

00**0 root du -s /var/* | sort -nr | mail -s 'Var Directory Usage' address@domain.com
 
Old 03-13-2009, 03:28 PM   #6
jim-the_linux_guy
LQ Newbie
 
Registered: Mar 2009
Posts: 1

Rep: Reputation: 0
Don't forget the du command allows you to set the units (K, M, MB, G, etc.) the program should use when displaying the results. Although it is not as pretty as "du -hs *", it is better than "du -s *" (as long as the appropriate unit is selected), and the sort command produces the results you're looking for.

Try this:

du -sBM * | sort -nr

M specifies all results will be displayed in 1M units (1024x1024). See man du for additional valid units.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check free disk space TigerLinux Linux - Newbie 9 10-04-2005 05:04 PM
Yum - check for disk space maimonoded Linux - Software 0 03-31-2005 09:23 AM
3Gb of disk space lost! Disk space problem or mother board conflicts with HDD Mistreated Linux - Hardware 4 12-06-2004 03:58 PM
Not enough space for root directory on a drive with 50g free space??? auoq Linux - Newbie 1 10-13-2004 12:44 PM
Boot disk; check. CD in drive; check. Doesn't work; check. Hal DamnSmallLinux 7 02-04-2004 02:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:33 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