1.Check Disk Space
#!/bin/bash
df -h | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $6 }' | while read output;
do
echo $output
if [ $(echo $output | awk '{print $1}' | sed 's/%//g') -ge 80 ]; then
echo "Warning: $output"
fi
done
This Bash script detects partitions on your Linux system with disk usage exceeding 80% and gives a warning. • Disk usage information is read with the df -h command. • Unnecessary lines (Filesystem, tmpfs, cdrom) are filtered with the grep command. • Disk fullness percentage and mount point are parsed with awk. • If the percentage value is 80% or higher, a warning message is displayed for the relevant volume. In this way, it allows you to notice in advance when disk fullness reaches critical levels.
2.Monitoring System Load
#!/bin/bash
uptime | awk -F 'load average:' '{print $2}' | sed 's/,//g'
It takes only the load average values from the system's uptime information, clears them and displays them. • The uptime command gives how long the system has been on and averages of CPU load. • The load average part is parsed with awk. • With sed, commas between load values are removed. It simply presents the instantaneous load averages of the system.
3.Find and Delete Files of a Specific Size
#!/bin/bash
find /path/to/search -type f -size +100M -exec rm -f {} \;
Finds and deletes files larger than 100 MB in the specified directory (/path/to/search). • The find command searches for a file in a specific directory. • -type f finds only files. • -size +100M targets files larger than 100 MB. • -exec rm -f {} ; Forcefully deletes found files. Must be used with caution!!, it automatically cleans large files.
4.List and Finalize Processes by Name
#!/bin/bash
ps aux | grep 'process_name' | awk '{print $2}' | xargs kill
This Bash command finds running processes with a specific process_name and kills them. • With ps aux, all running processes are listed. • The process_name specified with grep is filtered. • Process IDs (PID) are obtained with awk. • xargs kill takes these PIDs and kills the relevant processes. In short, it automatically terminates all processes bearing the specified process name.
5.Monitoring a Specific Port
#!/bin/bash
lsof -i :80
Lists all open files or processes using port 80. • lsof (list open files) shows open files on the system and the processes accessing these files. • The -i :80 option filters out processes that use only port 80 (usually used for HTTP traffic). It allows you to see the processes that are listening or using port 80.
6. Compressing a Folder
#!/bin/bash
tar -czvf archive_name.tar.gz /path/to/directory
Converts a specified directory into a compressed archive file. • tar is the archive creation command. • -czvf options: • c: creates a new archive. • z: Compresses with gzip. • v: shows the process in detail (verbose). • f: specifies the name of the archive file. • archive_name.tar.gz is the name of the archive to be created. • /path/to/directory is the path to the directory to be compressed. The specified directory is converted into a compressed archive file in .tar.gz format.
7. Extracting Files
#!/bin/bash
tar -xzvf file_name.tar.gz
Uncompresses a .tar.gz archive file and extracts its contents. • tar is used for archive operations. • -xzvf options: • x: extracts the archive. • z: Unpacks archives compressed with gzip. • v: shows the process in detail (verbose). • f: specifies the name of the archive file to be opened. • file_name.tar.gz is the name of the archive file to be opened. All contents in file_name.tar.gz are extracted to the current directory.
8. Bulk Rename Files
#!/bin/bash
for file in *.jpg; do
mv "$file" "${file%.jpg}_renamed.jpg"
done
This Bash command renames all files with .jpg extension in the current directory and adds “_renamed” to each filename. • for file in *.jpg; loop selects all .jpg files in the directory. • mv “$file” “${file%.jpg}_renamed.jpg” command moves each file with the new name, preserving its original name, adding the “_renamed” suffix. As a result, all .jpg files are renamed by adding a new name “_renamed”. For example, the file image.jpg is changed to image_renamed.jpg.
9. Backing up MySQL Database
#!/bin/bash
mysqldump -u root -p database_name > backup_file.sql
Creates a backup of the specified MySQL database. • mysqldump: Used to take backups of MySQL databases. • -u root: connects to the database with the root user. • -p: indicates that it will require a password. • database_name: is the name of the database to be backed up. • > backup_file.sql: specifies the name of the backup file and where to save it. The backup of the database named database_name is saved in the backup_file.sql file.
10. Clearing System Logs
#!/bin/bash
find /var/log/ -type f -mtime +30 -exec rm -f {} \;
This Bash command finds and deletes all files in the /var/log/ directory that are older than 30 days. • find /var/log/: This command searches for a file in the specified directory. • -type f: Targets files only (excluding directories). • -mtime +30: Finds files whose last modification date is older than 30 days. • -exec rm -f {} ;: Force (-f) deletes found files. As a result, log files older than 30 days in the /var/log/ directory are automatically purged.
11.Monitoring CPU Usage
#!/bin/bash
top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}'
Calculates and displays the current CPU usage percentage of the system. • top -bn1: Shows CPU and other system information once. • grep “Cpu(s)”: Filters CPU usage information in the output. • sed: Extracts the percentage value (id) at which the CPU is idle. • awk ‘{print 100 - $1”%”}’: Calculates the percentage of CPU used by subtracting the percentage of idle CPU from the total percentage. It simply shows the current usage percentage of the CPU.
12.List Empty Folders
#!/bin/bash
find /path/to/search -type d -empty
Finds all empty subdirectories in the specified directory. • find /path/to/search: Searches in the specified directory. • -type d: Targets directories only. • -empty: Finds empty directories. Lists all empty directories in /path/to/search that do not contain any files or subdirectories.
13.Monitor Memory Usage
#!/bin/bash
free -m | grep Mem | awk '{print "Memory Usage: " $3/$2 * 100 "% Used, " $3/1024 " GB Used, " $2/1024 " GB Total"}'
It calculates and displays the RAM usage in the system as a percentage and in GB. • free -m: Shows RAM usage information in megabytes (MB). • grep Mem: Filters only the line related to RAM. • awk: Calculates RAM usage and outputs as follows: • % Used: Calculates the ratio of used RAM to total RAM as a percentage. • GB Used: Shows the amount of RAM used in gigabytes. • GB Total: Shows the total RAM capacity in gigabytes. As a result, it allows you to see the amount of RAM used in the system, both in percentage and GB.
14.SSH Port Control
#!/bin/bash
nc -zv localhost 22
It checks whether port 22 on localhost (local machine) is open. • nc (netcat) is a tool used to create and control network connections. • -z: Scans the port, does not send data. • -v: Provides verbose output (verbose). • localhost 22: Controls port 22 on the local machine (usually used for SSH). This command reports whether port 22 on the local machine is open.
15. Failed Service Monitoring and Automatic Restart
#!/bin/bash
SERVICE="myapp"
while true; do
if ! systemctl is-active --quiet $SERVICE; then
echo "Service $SERVICE is down, attempting to restart."
systemctl restart $SERVICE
sleep 10
else
echo "Service $SERVICE is running."
sleep 60
fi
done
This Bash script checks whether a particular service, myapp, is constantly running and tries to restart it if the service is not running. • SERVICE=“myapp”: Specifies the service name to be controlled. • while true; do: Starts an infinite loop so the service is constantly checked. • systemctl is-active –quiet $SERVICE: Checks whether the service is active or not. • If the service is not running, it warns with echo and uses the systemctl restart $SERVICE command to restart the service. • If the service is running, it gives a message every 60 seconds indicating that the service is running. • sleep 10 and sleep 60: Sets wait times for restart attempts and normal check intervals. As a result, this script constantly monitors the myapp service and automatically restarts it if the service goes down.
16.Backup and Compress MySQL Database
#!/bin/bash
DB_NAME="mydatabase"
DB_USER="root"
DB_PASS="mypassword"
BACKUP_DIR="/backups"
DATE=$(date +%Y-%m-%d_%H%M%S)
FILENAME="$BACKUP_DIR/$DB_NAME-$DATE.sql.gz"
mysqldump -u $DB_USER -p$DB_PASS $DB_NAME | gzip > $FILENAME
echo "Database backup completed: $FILENAME"
This Bash script takes a compressed backup of a MySQL database and saves it in a specified directory. • DB_NAME=“mydatabase”, DB_USER=“root”, DB_PASS=“mypassword”: Name, username and password of the database to be backed up. • BACKUP_DIR=”/backups”: Directory where backups will be saved. • DATE=$(date +%Y-%m-%d_%H%M%S): Creates a timestamp containing the date and time information when the backup was taken. • FILENAME=”$BACKUP_DIR/$DB_NAME-$DATE.sql.gz”: The compressed file name of the backup. • mysqldump | gzip > $FILENAME: Backs up the database and compresses the resulting SQL file with gzip and saves it in the specified file. • echo: Displays the success message when the backup process is completed. As a result, this command takes a compressed backup of the mydatabase database and saves it as a date-stamped file in the /backups directory.
17.Find and Delete Old Files
#!/bin/bash
find /path/to/files -type f -mtime +30 -exec rm {} \;
echo "Deleted files older than 30 days."
It finds and deletes files in the specified directory that are older than 30 days, then notifies you that the deletion is complete. • find /path/to/files: Searches within the specified directory. • -type f: Targets files only (excludes directories). • -mtime +30: Finds files whose last modification date is older than 30 days. • -exec rm {} ;: Deletes found files. • echo “Deleted files older than 30 days.”: Provides an information message after the deletion process is completed. As a result, files older than 30 days in the specified directory are purged and the user is notified that the operation was successful.
18. Analyze Log Files and Send Alert Emails
#!/bin/bash
ERROR_COUNT=$(grep -c "ERROR" /var/log/myapp.log)
if [ $ERROR_COUNT -gt 0 ]; then
echo "Detected $ERROR_COUNT errors in the log file." | mail -s "Error Alert" admin@example.com
fi
This Bash script counts the words “ERROR” in a log file and if errors are detected, it sends an alert via email. • grep -c “ERROR” /var/log/myapp.log: Searches for the word “ERROR” in the log file and counts how many times it occurs. • if [ $ERROR_COUNT -gt 0 ]: If the number of errors is greater than zero (that is, there is an error), the action is taken. • echo “Detected $ERROR_COUNT errors in the log file.”: Creates a message containing the number of errors. • mail -s “Error Alert” [email protected]: Sends the error message by e-mail to the e-mail address specified with the title “Error Alert”. This script automatically sends a warning e-mail to the system administrator when it detects an error in the log file.
19. Generate SSH Key Pair Dynamically
#!/bin/bash
KEY_DIR="/home/user/.ssh"
KEY_NAME="mykey"
mkdir -p $KEY_DIR
ssh-keygen -t rsa -b 4096 -f $KEY_DIR/$KEY_NAME -N ""
echo "SSH key pair generated at $KEY_DIR/$KEY_NAME"
Creates a new SSH key pair in the specified directory. • KEY_DIR=”/home/user/.ssh”: Directory where SSH keys will be saved. • KEY_NAME=“mykey”: The name of the SSH key pair to be generated. • mkdir -p $KEY_DIR: Creates directory if it does not exist. • ssh-keygen -t rsa -b 4096 -f $KEY_DIR/$KEY_NAME -N “”: Generates a new SSH key pair with a length of 4096 bits using the RSA algorithm. -N “” creates a key without a password (empty password). • echo “SSH key pair generated at $KEY_DIR/$KEY_NAME”: Displays the message informing that the key pair was successfully generated. A 4096-bit SSH key pair is created in the specified directory and the user is informed about its location.
20.Bulk Changing File Extensions
#!/bin/bash
for file in *.txt; do
mv "$file" "${file%.txt}.md"
done
echo "File extensions changed from .txt to .md"
Converts the names of files with .txt extension in the current directory to .md extension. • for file in *.txt;: Operates on all files with .txt extension in the directory. • mv “$file” “${file%.txt}.md”: Changes the extension of each file from .txt to .md. • echo: After the process is completed, notifies you that the file extensions have been changed successfully. As a result, all .txt files are converted to .md extension and the success message is displayed.
21. Periodically Poll API and Process Response
#!/bin/bash
while true; do
RESPONSE=$(curl -s http://api.example.com/data)
process_response "$RESPONSE"
sleep 60
done
process_response(){
# Process the response here
echo "$1"
}
It continuously retrieves data from a specified API and processes the response every 60 seconds. • while true; do: Starts an infinite loop, continuously pulling data. • RESPONSE=$(curl -s http://api.example.com/data): Retrieves data from the API and saves the response in the RESPONSE variable. The -s option makes curl run in silent mode (returning only output). • process_response “$RESPONSE”: Sends the API response to the process_response function. • sleep 60: Queries the API again every 60 seconds. • process_response(): A function to process the response. Currently it just prints the data it receives to the screen (echo “$1”), but more complex processing steps can be added to this function. This script receives data from the API every minute, processes it and prints it to the screen. The response processing part can be customized within the function.
22. Check Disk Space and Clean Temporary Files Automatically
#!/bin/bash
FREE_SPACE=$(df / | grep / | awk '{print $5}' | sed 's/%//g')
if [ $FREE_SPACE -le 10 ]; then
echo "Low disk space, cleaning up temporary files..."
rm -rf /tmp/*
echo "Temporary files cleaned up."
fi
This Bash script checks the free disk space in the root directory and if the free space is 10% or less, it clears the disk space by deleting temporary files in the /tmp directory. • FREE_SPACE=$(df / | grep / | awk ‘{print $5}’ | sed ‘s/%//g’): Gets the disk usage rate in the root directory and saves it as a numeric value by removing the percent sign. • if [ $FREE_SPACE -le 10 ]; then: If the free space is 10% or less, it performs the following operations. • echo “Low disk space, cleaning up temporary files…”: Writes the low disk space warning on the screen. • rm -rf /tmp/*: Deletes all temporary files in the /tmp directory. • echo “Temporary files cleaned up.”: Notifies the user after the cleaning process is completed. As a result, this script frees up space by automatically clearing temporary files when the root directory is critically low on disk space.
23.Automatic Deployment Script
#!/bin/bash
# Assuming Git is used for version control
git pull
# Build the project (e.g., using Maven)
mvn clean install
# Stop the currently running service
systemctl stop myapp
# Deploy the newly built app
cp target/myapp.jar /var/lib/myapp/
# Restart the service
systemctl start myapp
echo "Deployment completed successfully."
It pulls a project from Git, compiles it, and then restarts the relevant service by deploying the new version. • git pull: Pulls the latest changes from Git, which is used as the version control system. • mvn clean install: Cleans and recompiles the project using Maven (for example, for Java projects). • systemctl stop myapp: Stops the currently running myapp service. • cp target/myapp.jar /var/lib/myapp/: Copies the new version of the compiled application (myapp.jar) to the target directory. • systemctl start myapp: Restarts the service with the new version. • echo “Deployment completed successfully.”: Indicates that the deployment process was completed successfully. It updates and redeploys the project and runs the service with the updated version without interruption.
24.Processing CSV Files with awk
#!/bin/bash
awk -F, '{if ($1=="John Doe") print $2}' input.csv
In the input.csv file, extracts the data from the second column of the rows containing the name John Doe and prints it to the screen. • awk -F,: Processes columns separated by commas (,). • if ($1==“John Doe”): Filters rows whose first column (usually the name) is John Doe. • print $2: Prints the second column in these lines to the screen. Lists the data of the person named John Doe in the input.csv file in the second column.
25. Bulk Changing File Permissions
#!/bin/bash
for file in /path/to/files/*; do
chmod 644 "$file"
done
echo "Permissions updated for all files in /path/to/files/"
Sets the permissions of all files in the specified directory to 644. • for file in /path/to/files/*: Loops all files in the specified directory. • chmod 644 “$file”: Changes the permissions of each file to 644. This gives the file owner read and write permissions and the group and other users only read permissions. • echo “Permissions updated for all files in /path/to/files/”: Displays an information message when the process is completed. The permissions of all files in the specified directory are updated and notified to the user.
26.Use curl and jq to Process JSON API Response
#!/bin/bash
response=$(curl -s https://api.example.com/data)
echo "$response" | jq '.items[].name'
It processes the JSON response received from an API and extracts the values of a specific field, name, and prints it to the screen. • curl -s api.example.com/data: Queries the API in silent mode and receives a response in JSON format. • jq ‘.items[].name’: Takes all name fields in the items array in the JSON response and prints them to the screen. The jq command is used to process JSON data. The values of all name fields in the API response are listed.
27.Monitor Network Bandwidth Usage
#!/bin/bash
ifconfig eth0 | grep 'RX bytes' | awk '{print $2 $3}' | cut -d: -f2 | sed 's/ //g'
ifconfig eth0 | grep 'TX bytes' | awk '{print $2 $3}' | cut -d: -f2 | sed 's/ //g'
Extracts and displays the amount of data received (RX) and sent (TX) for the eth0 network interface. • ifconfig eth0: Returns information of the network interface named eth0. • grep ‘RX bytes’: Finds the line containing the received bytes (RX) information. • awk ‘{print $2 $3}’: Gets the second and third columns in this row (for example, RX bytes:123456). • cut -d: -f2: splits by : character and selects the second part (RX byte value). • sed ‘s/ //g’: Removes spaces and retrieves only the byte value. The same operations are performed for TX bytes (sent data). The total amount of data received and sent from the eth0 interface is printed separately on the screen.
28.Recursively Search and Replace Text in Files
#!/bin/bash
find /path/to/search -type f -exec sed -i 's/old_text/new_text/g' {} +
echo "All occurrences of 'old_text' replaced with 'new_text'."
Replaces “old_text” with “new_text” in all files in the specified directory. • find /path/to/search -type f: Finds all files in the specified directory. • -exec sed -i ‘s/old_text/new_text/g’ {} +: Replaces old_text with new_text using the sed command in each file. The -i option edits the files in-place, while the g flag replaces all matches found on each line. • echo “All occurrences of ‘old_text’ replaced with ‘new_text’.”: Displays a message informing that the operation was completed successfully. All instances of old_text in all files in the specified directory are replaced with new_text and an information message is given to the user.
29.Compare Two Directories with diff
#!/bin/bash
diff -rq /dir1 /dir2
It compares two directories /dir1 and /dir2 and lists the differences between them. • diff -rq: Compares two directories. • -r: Compares directories recursively, that is, including subdirectories. • -q: Only gives information about different files and directories, does not show detailed content differences. This command summarizes the differences (missing, different, or non-identical files) between the /dir1 and /dir2 directories.
30. Scheduled Cleanup Script for /tmp Directory
#!/bin/bash
find /tmp -type f -mtime +7 -exec rm -f {} \;
echo "/tmp cleaned up of files older than 7 days."
It deletes all files in the /tmp directory that are older than 7 days and gives an information message after the process is completed. • find /tmp -type f -mtime +7: Finds files in the /tmp directory whose last modification date is older than 7 days. • -exec rm -f {} ;: Forcefully deletes found files (-f). • echo “/tmp cleaned up of files older than 7 days.”: Displays a message indicating that the cleaning process is complete. As a result, files older than 7 days in the /tmp directory are automatically purged and the user is notified.
31.Create User and Set Password (Using chpasswd)
#!/bin/bash
echo "username:password" | sudo chpasswd
sudo useradd -m username
echo "User 'username' created and password set."
This Bash script creates a new user and sets a password for this user. • echo “username:password” | sudo chpasswd: Gets the specified username and password and sets the password for this user with the chpasswd command. • sudo useradd -m username: Creates a new user using the specified username and creates the user's home directory (-m). • echo “User ‘username’ created and password set.”: Provides an information message after the user has been successfully created and the password has been set. This script creates a new user and automatically sets a password for it.
32.Monitor and Alert Disk I/O (using iostat and mail)
#!/bin/bash
iowait=$(iostat -dx 1 2 | tail -1 | awk '{print $11}')
if [ "$iowait" -gt 30 ]; then
echo "High IO wait ($iowait%) detected." | mail -s "IO Wait Warning" admin@example.com
fi
It checks the percentage of IO wait (input/output waiting time) in the system and sends an alert email if it exceeds a certain threshold. • iowait=$(iostat -dx 1 2 | tail -1 | awk ‘{print $11}’): Runs the iostat command in two loops and gets the IO wait value (in %) in the last line. • if [ “$iowait” -gt 30 ]; then: If IO wait is greater than 30%, it performs the following operation. • echo “High IO wait ($iowait%) detected.” | mail -s “IO Wait Warning” [email protected]: Sends a warning message containing the IO wait percentage with a subject heading to a specified email address. As a result, this script automatically detects high IO wait situations and sends an alert so that a possible slowdown or performance issue in the system can be noticed.
33.Sync files with rsync
#!/bin/bash
rsync -avz /source/ /destination/
echo "Files synchronized from /source/ to /destination/."
This Bash script synchronizes the files in the /source/ directory to the /destination/ directory using the rsync command and gives an informational message after the process is completed. • rsync -avz /source/ /destination/: • -a: Runs in archive mode, preserving all file permissions, timestamps and directory structure. • -v: Provides verbose output (verbose), shows the file copying process. • -z: Transfers files by compressing them (if synchronizing over the network). • /source/: Source directory. • /destination/: Destination directory. • echo “Files synchronized from /source/ to /destination/.”: Gives an information message to the user after synchronization is completed. As a result, this script synchronizes the files in the /source/ directory to the /destination/ directory and notifies the user that the operation completed successfully.
34.Check Port Status and Log Result
#!/bin/bash
if nc -z localhost 80; then
echo "Port 80 is open."
else
echo "Port 80 is closed."
fi
It checks whether port 80 on localhost is open and gives a message accordingly. • nc -z localhost 80: Checks whether port 80 is open on localhost using netcat. The -z flag scans the port and sends no data. • if … then … else: If the port is open, the message “Port 80 is open” is displayed, if it is closed, the message “Port 80 is closed” is displayed. As a result, this script checks the status of port 80 on localhost and returns an appropriate message if the port is open or closed.
35. Dynamically Create and Manage Docker Containers
#!/bin/bash
CONTAINER_NAME="myapp_container"
IMAGE="myapp:latest"
if docker ps -aqf "name=^/$CONTAINER_NAME$"; then
echo "Container $CONTAINER_NAME already exists. Restarting..."
docker restart $CONTAINER_NAME
else
echo "Creating and starting new container $CONTAINER_NAME..."
docker run -d --name $CONTAINER_NAME $IMAGE
fi
This Bash script controls a specified Docker container; If the container already exists it restarts it, otherwise it creates and starts a new container. • CONTAINER_NAME=“myapp_container”: The name of the container to control or create. • IMAGE=“myapp:latest”: Docker image to use. • docker ps -aqf “name=^/$CONTAINER_NAME$”: This command checks whether a container with the specified name exists. • -a: Lists all containers (running and stopped). • -q: Returns only container IDs. • -f “name=…”: Filters with the specified name. • If the container exists, the container is restarted with docker restart $CONTAINER_NAME. • If the container does not exist, a new container is started from the specified image with the docker run -d –name $CONTAINER_NAME $IMAGE command. As a result, this script restarts an existing Docker container or creates and starts a new one if the container does not exist.
BONUS! - Bulk Download Web Pages and Save as HTML Files
#!/bin/bash
URLS=("http://example1.com" "http://example2.com")
for url in "${URLS[@]}"; do
filename=$(echo "$url" | tr -d '/' | sed 's|^http://||;s|$|.html|')
curl -o "$filename" "$url"
done
It downloads content from specified URLs and saves the contents in these files by editing the file names for each URL. • URLS=(“http://example1.com”" title="http://example1.com”" style="font-weight:bold; text-decoration:underline; color: var(--primary);" target="_blank">http://example1.com” “http://example2.com”): An array containing the URLs to download. • for url in “${URLS[@]}”; do: Loops over each element in the URL array. • filename=$(echo “$url” | tr -d ‘/’ | sed ‘s|^http://||;s|$|.html|’): Edits the URL as a filename. • tr -d ‘/’: Removes forward slashes in the URL. • sed ’s|^http://||: Removes the “http://” part from the URL. • s|$|.html|: Adds “.html” extension to the file name. • curl -o “$filename” “$url”: Downloads content from the URL and saves this content in a file called filename. This script downloads content from specified URLs and saves each URL in files specified by its name. For example, the content of http://example1.com is saved in the file named example1.com.html.
We hope the scripts here were useful to you. Please do not forget to follow our page and comment on the content for more.