Take the 2-minute tour ×

I am trying to wrote a script to check homefolders disk usage, and warn users when they are above xxGB, by email

I dump the output of the du -s * to a temp file, read it line by line, and when i try to read the size and name of the folder from the output of du it does not work correctly, just doing an echo $file i get each line dumped as two lines, i tried expand to replace the tabs with spaces, but also didn't work, and I am not sure how to do the comaprison based on size too.


#echo "Disk usage report for /homes on `hostname`"


# check staff

cd /homes/staff/
du -s * | sort -rn | head -15  |awk '{print}' > $file1
expand $file1 > $file2

for line in $(cat $file2)


echo $line

# echo $line | awk '{ print $2 }'

mail -s "Disk usage report for your homefolder" $EMAIL

share|improve this question
Why not just implement quotas? –  Ignacio Vazquez-Abrams Apr 5 '11 at 14:59
it was very expensive, we are running virtual machines, and when we implemented quotas, the load on the servers from the du being run on every user login was very expensive, so we implmented a simple script that runs once a week, but now we want to enhance it,, –  Judy Apr 5 '11 at 15:13

4 Answers 4

Why not simply implement disk quotas? Almost all Unix/Linux systems can do that.

However, if you're really want to do it this way, why all the machinations?

The du - s * will produce a two column output with diskspace used and user name. Use a while loop instead of putting everything in temporary files.

cd /home   #Or where ever all the user's home directories are stored
du -s | while read space user
    if [ $space -gt 10000000 ]
        mailx -s"You're using a lot of diskspace!" $user <<MAIL
Dear $user:

We notice that you are now using $space in your home directory.
are you storing there? The total amount of diskspace allowed
is 15,000,000. We highly suggest you trim down your diskspace, or
we'll do it for you.


Your Kindly System Administrator
share|improve this answer
thanks, this is exactly what we want,but am getting an error, syntax error unexpected end of file –  Judy Apr 5 '11 at 17:50
You can add "set -xv" to the front of the shell script. This will print out each line and the value of each environment variable. It helps with debugging issues like this. The issue could be the HEREIS document (The "<<"). Try replacing the lines between the line that starts with "mailx" and end with the line with MAIL all by itself (including those two lines) with something like this "echo User = $user Space = $space". If that gets rid of the EOF error, you'll know it's an issue with the hearis document. –  David W. Apr 5 '11 at 21:25

The for loop is tokenizing your input based on spaces. So each word becomes a $line.

Instead of for loop, you can use a while loop to capture the input correctly, e.g.

cat $file2 | while read line; do echo $line; done

(You could add set -x to your script temporarily to see what's happening)

share|improve this answer

Have a look at durep

Install durep in Ubuntu

Use the following command to install durep

sudo aptitude install durep

Using durep

Syntax is roughly durep [OPTION]… [DIRECTORY]

  • “durep -w ~/durepweb -td 2″

    This would print the directory tree starting from the current directory to depth 2 to the console and also create web pages in the directory ~/durepweb (this directory must exist).

share|improve this answer
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. –  Junuxx Nov 14 '12 at 13:37
@Junuxx Good point. I must have forgotten. Also, I was only starting out on SO back in the days :). Added some usage examples –  sehe Nov 14 '12 at 13:45
That was an automatic comment since this came up in the Low Quality review queue. But it's much better now! :) –  Junuxx Nov 14 '12 at 13:47

If you ask for

 du -s /home/joe/* 

you get a summary for all files (except hidden files) and directories one by one, because the * is expanded by the shell.

 du -s /home/joe

will give you a single line, everything summed up, including hidden files.

 du -s . 

would summarize the whole directory too - and would include hidden files (just tested it).

Since it will just be one line, the whole line collapses from



 du -s . >$file1 

because you don't need to sort a single line, reduce it to 15 lines, and repeat it with the awk-statement.

share|improve this answer
am already doing that, but now as i try to parse it, if the file list is as follows: –  Judy Apr 5 '11 at 15:42
Hm. du -s . > $file1 or du -s /home/joe > $file1 produce empty files? –  user unknown Apr 5 '11 at 16:07

Your Answer


