Chapter 26

Writing Script Utilities

In This Chapter

  • Monitoring disk space
  • Performing backups
  • Managing user accounts

There is no place where shell script programming is more useful than writing script utilities for the Linux system administrator. The typical Linux system administrator has a myriad of jobs that need to be done daily, from monitoring disk space to backing up important files to managing user accounts. Shell script utilities can make the life of the system administrator much easier! This chapter demonstrates some of the capabilities you have writing script utilities in the bash shell.

Monitoring Disk Space

One of the biggest problems with multi-user Linux systems is the amount of available disk space. In some situations, such as in a file sharing server, disk space can fill up almost immediately just because of one careless user.

This shell script utility helps you determine the top ten disk space consumers for designated directories. It produces a date-stamped report that allows disk space consumption trends to be monitored.

The Required Functions

The first tool you need to use is the du command (see Chapter 4). This command displays the disk usage for individual files and directories. The -s option lets you summarize totals at the directory level. This comes in handy when calculating the total disk space used by an individual user. Here's what it looks like to use the du command to summarize each user's $HOME directory for the /home directory contents:

$ du -s /home/*  ...

Get Linux® Command Line and Shell Scripting Bible, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.