Friday, January 18, 2008

HowTo: Sort Files by FileSize and What Else?

If you have thousands of email users and you wish to get email alerts of the top 10 largest mail box users from your email without using any other external linux software, here's how to accomplish this task. This task can also be used to check for top largest files of your hard drive from linux terminal command line.

Basically, in order to determine the top or the largest ones, we need to cover first how to sort files by their filesizes. This is the first basic step to determine the largest mail box user or the largest filesize from the rest of the list.

Considering that database is not part of your mail server, consider this approach to determine your largest file from your hard drive or top mail box users on flat file email system.

Sorting Files by FileSizes

By issuing the below command, you get to display sorted files from highest to lowest order
# cd /var/mail
# ls -lS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-rw------- 1 rtolosa mail 899999958 Jan 18 11:41 rtolosa
-rw------- 1 ayusuf mail 899999937 Jan 18 11:41 ayusuf
-rw------- 1 aaluko mail 899999916 Jan 18 11:41 aaluko
-rw------- 1 emarquez mail 899999713 Jan 18 11:41 emarquez
-rw------- 1 aogiri mail 899999617 Jan 18 11:41 aogiri
-rw------- 1 psmith mail 899999599 Jan 18 11:41 psmith
..
<..snipped>
...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Alternatively, one quick and dirty approach that would give you the same above result would be
# ls -l | sort -k5 -nr

or

# ls -ls /var/spool/mail/ | du -h /var/spool/mail/* | sort -nr | grep "M"

Now, how can make this data and approach be of useful to our daily tasks. Let us assume we want to get email alerts of these top 5 or top 10 largest file user to our mail box, or how about getting the largest mail spool user on daily basis into our mail box?


Get Email Alerts from Script Outputs

Here's how to get email alerts of top flat mail box users (without database) or top file users?

Simply get the top 5 or top 10 largest file from your system or largest flat mail box user, simply issue the linux head command

Get Top N Largest User

For top 5 largest file or largest mail box user
# cd /var/mail
# ls -l | sort -k5 -nr | head -5
# ls -lS | head -5
Result:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-rw------- 1 rtalasa mail 899999958 Jan 18 11:41 rtalasa
-rw------- 1 ayesuf mail 899999937 Jan 18 11:41 ayesuf
-rw------- 1 aaluko mail 899999916 Jan 18 11:41 aaluko
-rw------- 1 amarquez mail 899999713 Jan 18 11:41 amarquez
-rw------- 1 aogiri mail 899999617 Jan 18 11:41 steve
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ls -ls /var/spool/mail/ | du -h /var/spool/mail/* | sort -nr | grep "M" | head -5
Result:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
860M /var/spool/mail/yabdulrahman
860M /var/spool/mail/vogobiri
860M /var/spool/mail/rtolosa
860M /var/spool/mail/rlinog
860M /var/spool/mail/raouad
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Of course, the top 10 is similarly like so
# cd /var/mail
# ls -l | sort -k5 -nr | head -10
# ls -lS | head -10
# ls -ls /var/spool/mail/ | du -h /var/spool/mail/* | sort -nr | grep "M" | head -10

Now, a more simplified form of retrieving the largest user from /var/spool/mail, parsing and retrieving only the username and the filesize fields could be done like so

# ls -l | sort -k5 -nr | head -5 | awk '{print $3,$5}'


If you wish to alert yourself and send these results to your email box , simply save the result as file and send the file to your email similarly like so

# ls -l | sort -k5 -nr | head -5 | awk '{print $3,$5}' > top5
# cat top5 | mail -s "My Top Large Files or Users" myown@domain.com


Job Scheduling

Remember the cron scheduling job to do it on regular basis.

Enjoy.

Related Readings:
Cronjob Scheduling Explained
GNOME Task Cron Scheduler
KDE GUI Task Scheduler

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy