5 Useful Linux Scripts: Enhancing Your Linux Experience

5 Useful Linux Scripts: Enhancing Your Linux Experience

Linux is a powerful operating system loved by developers and system administrators alike for its flexibility and control. While the command line may seem daunting at first, it offers immense power when paired with well-crafted scripts. This article will explore five incredibly useful Linux scripts that can save you time and enhance your workflow. Each script will be explained in detail, with clear instructions on how to create and use them. Whether you’re a beginner or a seasoned Linux user, these scripts are invaluable tools for your toolkit.


1. Creating a Colorful Script

A colorful script can make command-line outputs more readable and visually appealing. This script allows you to add colors to your output text, making important information stand out.

Script Explanation:

In Linux, you can use ANSI escape codes to change text color. This script defines these codes as variables and then uses them to print colorful text.

Script Example:

#!/bin/

# Define color variables
RED='33[0;31m'
GREEN='33[0;32m'
YELLOW='33[1;33m'
BLUE='33[0;34m'
NC='33[0m' # No Color

# Print text in different colors
echo -e "${RED}This is red text${NC}"
echo -e "${GREEN}This is green text${NC}"
echo -e "${YELLOW}This is yellow text${NC}"
echo -e "${BLUE}This is blue text${NC}"

# Notify completion
echo -e "${GREEN}Script completed successfully!${NC}"

How It Works:

  • 33[0;31m sets the text color to red.
  • 33[0m resets the color back to default.
  • ${RED}, ${GREEN}, etc., are variables storing these color codes.
  • echo -e is used to enable interpretation of backslash escapes.

Usage:

  • Save the script with a .sh extension.
  • Make it executable with the command:
chmod +x yourscript.sh 
  • Run the script:
./yourscript.sh 

This simple script makes outputs more engaging and easier to read, especially useful in logs or reports.

Creating a Colorful Script


2. Linux Server Monitor Script

Monitoring your server’s health is crucial in maintaining performance and avoiding downtime. This script provides a quick snapshot of the server’s CPU, memory, disk usage, and active processes.

Script Explanation:

This script uses common Linux commands to gather system information and display it in a readable format.

Script Example:

#!/bin/

echo "Server Health Report"
echo "===================="

# Display current date and time
echo "Date & Time: $(date)"

# CPU usage
echo "CPU Usage:"
top -bn1 | grep "Cpu(s)" | sed "s/.*, *([0-9.]*)%* id.*/1/" | awk '{print 100 - $1"% used"}'

# Memory usage
echo "Memory Usage:"
free -m | awk 'NR==2{printf "Used: %sMB (%.2f%%)n", $3, $3*100/\ }'

# Disk usage
echo "Disk Usage:"
df -h | awk '$NF=="/"{printf "Used: %d/%dGB (%s)n", $3, $2, $5}'

# Active processes
echo "Top 5 Memory Consuming Processes:"
ps aux --sort=-%mem | awk 'NR<=5{printf "%-10s %-10s %sn", $2, $4, $11}'

echo "===================="
echo "End of Report"

How It Works:

  • date shows the current date and time.
  • top -bn1 gets CPU usage. We filter and format the output to show the percentage used.
  • free -m displays memory usage, and awk helps format the output.
  • df -h shows disk usage, and ps aux lists running processes sorted by memory usage.

Usage:

  • Save the script as server_monitor.sh.
  • Make it executable:
chmod +x server_monitor.sh 
  • Run the script:
./server_monitor.sh 

This script is handy for quick server health checks, giving you essential information in one glance.


3. Generate a List of IP Addresses

Sometimes, you need a quick way to list all the IP addresses connected to your server or network. This script will help you generate such a list.

Script Explanation:

The script uses networking commands to extract and display IP addresses from various sources.

Script Example:

#!/bin/

echo "List of IP Addresses"
echo "===================="

# List IP addresses associated with network interfaces
echo "Local IP Addresses:"
ip -o -4 addr list | awk '{print $4}' | cut -d/ -f1

# List IP addresses connected to the server
echo "Connected IP Addresses:"
netstat -tn | awk '{print $5}' | cut -d: -f1 | grep -v '0.0.0.0' | sort -u

echo "===================="
echo "End of List"

How It Works:

  • ip -o -4 addr list lists all IPv4 addresses on your machine.
  • netstat -tn lists active connections. We filter and extract the IP addresses.
  • sort -u ensures the list has unique entries.

Usage:

  • Save the script as ip_list.sh.
  • Make it executable:
chmod +x ip_list.sh 
  • Run the script:
./ip_list.sh 

This script is especially useful for network management and troubleshooting, allowing you to quickly see active IPs.


4. Disk Usage Analyzer Script

Knowing how your disk space is being used is crucial for system maintenance. This script provides a detailed breakdown of disk usage across directories.

Script Explanation:

The script uses the du command to analyze disk usage and then sorts the results to show the largest directories.

Script Example:

#!/bin/

echo "Disk Usage Analysis"
echo "===================="

# Analyze disk usage and sort by size
du -ah / | sort -rh | head -n 20

echo "===================="
echo "End of Analysis"

How It Works:

  • du -ah / calculates the disk usage of each file and directory under the root.
  • sort -rh sorts the results by size in human-readable format.
  • head -n 20 limits the output to the top 20 largest directories or files.

Usage:

  • Save the script as disk_usage.sh.
  • Make it executable:
chmod +x disk_usage.sh 
  • Run the script:
./disk_usage.sh 

This script is invaluable for identifying what’s consuming the most space on your system, allowing you to manage storage more effectively.


5. Internet Speed and Ping Test Script

This script allows you to test your internet speed and measure the ping to a specific server. It’s useful for diagnosing connectivity issues.

Script Explanation:

The script combines tools like ping and speedtest-cli to measure network performance.

Script Example:

#!/bin/

echo "Internet Speed and Ping Test"
echo "============================"

# Run a speed test
echo "Running Speed Test:"
speedtest-cli --simple

# Ping a server
echo "Ping Test to Google:"
ping -c 5 google.com

echo "============================"
echo "Test Completed"

How It Works:

  • speedtest-cli --simple runs an internet speed test, displaying download and upload speeds.
  • ping -c 5 google.com sends five ping requests to Google, measuring round-trip time.

Usage:

  • Save the script as network_test.sh.
  • Make it executable:
chmod +x network_test.sh 
  • Run the script:
./network_test.sh 

This script is a quick way to check your internet speed and latency, helping you diagnose network issues.

Internet Speed and Ping Test Script


Conclusion

These five Linux scripts are powerful tools that can greatly enhance your productivity and system management. Whether you’re looking to monitor server health, analyze disk usage, or troubleshoot network issues, these scripts offer simple yet effective solutions. By using these scripts, you can streamline your workflow, save time, and gain deeper insights into your Linux environment.

Fedya Serafiev

Fedya Serafiev

Fedya Serafiev owns the website linuxcodelab.eu. He finds satisfaction in helping people solve even the most complex technical problems. His current goal is to write easy-to-follow articles so that such problems do not arise at all.

Thank you for reading the article! If you found the information useful, you can donate using the buttons below: