5 Useful Bash Scripts for Everyday Tasks

5 Useful Bash Scripts for Everyday Tasks

Objectives:

  • Learn to write and understand practical Bash scripts for common automation tasks.

  • Develop scripts that interact with files, directories, logs, and system information.

  • Apply error handling, input validation, and logging to these scripts.


Chapter Outline:

1. Disk Usage Report

Script Overview:

This script generates a detailed disk usage report for the user, highlighting space usage by directories.

#!/bin/bash

# Check if directory path is provided
if [ -z "$1" ]; then
    echo "Usage: $0 <directory_path>"
    exit 1
fi

directory="$1"

# Check if the directory exists
if [ ! -d "$directory" ]; then
    echo "Directory not found: $directory"
    exit 1
fi

# Display disk usage report
du -sh "$directory"/* | sort -rh > disk_usage_report.txt
echo "Disk usage report saved to disk_usage_report.txt"

https://github.com/bc0de0/bash-scripts/blob/master/disc_usage.sharrow-up-right

Explanation:

  1. Argument Validation:

    • Checks if the user provides a directory argument.

    • Verifies if the directory exists.

  2. Disk Usage Report:

    • The du -sh command displays the disk usage of all items in the directory, and the result is sorted by size (sort -rh).

  3. Output to File:

    • The report is saved as disk_usage_report.txt.

Real-World Use:

This script can be used to monitor disk space usage and identify large files that need cleaning.


2. Monitoring Active Network Connections

Script Overview:

A script that monitors active network connections and logs them to a file for later analysis.

https://github.com/bc0de0/bash-scripts/blob/master/net_chk.sharrow-up-right

Explanation:

  1. netstat Command:

    • netstat -tunapl shows active network connections along with their listening ports (t for TCP, u for UDP, n for numeric IPs, a for all connections, p for programs).

  2. Logging:

    • The output of netstat is appended to network_connections.log, which includes a timestamp ($(date)).

Real-World Use:

This is useful for monitoring network activity, detecting potential intrusions, or identifying suspicious connections.


3. Automated Backup of Log Files

Script Overview:

A script to automate the backup of system log files.

https://github.com/bc0de0/bash-scripts/blob/master/log_backup.sharrow-up-right

Explanation:

  1. Directory Setup:

    • Checks if the backup directory exists; if not, it creates it.

  2. Backup and Compression:

    • Uses tar to archive and compress the log files into a .tar.gz file, saved with a timestamp for easy identification.

Real-World Use:

This script automates the backup of log files, ensuring important logs are archived regularly for auditing or forensic analysis.


4. Find and Kill Resource-Heavy Processes

Script Overview:

A script to find processes consuming too much CPU or memory and prompt the user to kill them.

https://github.com/bc0de0/bash-scripts/blob/master/proc_kill.sharrow-up-right

Explanation:

  1. Display Top CPU-Consuming Processes:

    • ps -eo pid,comm,%cpu lists processes along with their PID, command name, and CPU usage, sorted by CPU usage.

  2. User Input:

    • Prompts the user to decide whether to kill a resource-heavy process.

  3. Kill Process:

    • If a PID is provided, the script kills that process using kill -9.

Real-World Use:

This script helps system administrators manage server resources by identifying and terminating processes that consume excessive CPU or memory.


5. Automated System Health Check

Script Overview:

A script that checks system health by gathering key system information and logs it.

https://github.com/bc0de0/bash-scripts/blob/master/sys_health.sharrow-up-right

Explanation:

  1. System Information:

    • The script gathers uptime, memory usage (free -h), and disk usage (df -h).

  2. CPU Processes:

    • Captures the top 5 CPU-consuming processes.

  3. Logging:

    • All the gathered information is logged in a file for future reference.

Real-World Use:

This script is useful for performing quick system health checks, making it easier to monitor resource utilization on servers or workstations.


Last updated