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.
Explanation:
Argument Validation:
Checks if the user provides a directory argument.
Verifies if the directory exists.
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
).
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.
Explanation:
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).
Logging:
The output of
netstat
is appended tonetwork_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.
Explanation:
Directory Setup:
Checks if the backup directory exists; if not, it creates it.
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.
Explanation:
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.
User Input:
Prompts the user to decide whether to kill a resource-heavy process.
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.
Explanation:
System Information:
The script gathers uptime, memory usage (
free -h
), and disk usage (df -h
).
CPU Processes:
Captures the top 5 CPU-consuming processes.
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