Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Software
User Name
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.


  Search this Thread
Old 12-08-2012, 01:16 AM   #1
LQ Newbie
Registered: Jan 2012
Posts: 4

Rep: Reputation: Disabled
Help with a small bash script

Hey guys,

I want to mention that I'm still a beginner at this so bear with me

I made a small script that will check the users and if the size is larger than, let's say 204800, it will print the user and then send an email with the report.

Everything works except the fact that the script prints all the users, not only the one's having the value larger than 204800

This is the script, any suggestions are welcome:

#! /bin/bash
#finding all users, checking folder size and sorting

/usr/bin/find /home -printf "%u %s\n" | /usr/bin/awk '{user[$1]+=$2}; END { if (user[i] -gt 204800) for( i in user) print i " " user[i]}' | /bin/sort -rn +1 -2 > /root/disk-usage.txt;
#cheking if there is something in disk-usage.txt
if ls /root/ | grep -q "disk-usage.txt"
    # send email containing the output of the file
   /bin/cat /root/disk-usage.txt | mail -s "Disk Usage Report"


#delete txt file
rm -rf /root/disk-usage.txt
Old 12-08-2012, 05:31 AM   #2
Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
It is probably easier to just use

du -s /home/* | awk '{if($1 > 204800){print $0;}}' | sort -rn > /root/disk-usage.txt
'du' does all the counting for you, and then do the comparison. I think your awk might work if you put braces ("{" and "}") in it (currently, if you check the contents of /root/disk-usage.txt, then you can see it contains all users' usage, not the ones who have used too much), but it's probably simpler to use 'du' and then a smaller, clearer, awk command.

Hope this helps,
Old 12-08-2012, 05:49 AM   #3
LQ Newbie
Registered: Jan 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
That helped, thanks.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] small bash script m3phisto Linux - Newbie 7 04-26-2011 04:38 AM
Very small bash script enemorales Programming 8 02-25-2005 02:47 AM
small bash script issue zoomzoom Linux - General 7 06-08-2004 06:33 PM
small bash script to kill a PID flosch Linux - General 3 05-06-2004 09:28 PM
sed in small BASH script OhLordy Linux - General 1 08-29-2003 11:32 AM

All times are GMT -5. The time now is 03:04 PM.

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