Here’s a collection of some essential shell scripts, including practical examples that can be modified for real-world use. Each script comes with a brief description of what it does and how to use it.
1. Backup Files
#!/bin/bash # Backup specified directory to a target directory SOURCE_DIR="/path/to/source" BACKUP_DIR="/path/to/backup" rsync -av --delete "$SOURCE_DIR/" "$BACKUP_DIR/"
Description: This script uses rsync
to backup files from the source directory to the backup directory, deleting files in the backup that no longer exist in the source.
2. Disk Usage
#!/bin/bash # Display disk usage of specified directory DU_DIR="/path/to/directory" du -sh "$DU_DIR"
Description: This script outputs the total disk usage of the specified directory.
3. Find Large Files
#!/bin/bash # Find and list files larger than 100MB find / -type f -size +100M
Description: Searches the entire filesystem for files larger than 100MB.
4. Monitor System Load
#!/bin/bash # Monitor system load and alert if it exceeds a threshold THRESHOLD=1.0 LOAD=$(uptime | awk '{print $(NF-2)}' | sed 's/,//') if (( $(echo "$LOAD > $THRESHOLD" | bc -l) )); then echo "High system load: $LOAD" fi
Description: This script checks the current system load and alerts if it exceeds the defined threshold.
5. Create a Directory Tree
#!/bin/bash # Create a directory tree for project structure mkdir -p ~/Projects/{ProjectName/{src,bin,lib,doc},tests,logs}
Description: Creates a predefined directory structure for a new project.
6. Check for Running Processes
#!/bin/bash # Check if a process is running PROCESS_NAME="my_process" if pgrep "$PROCESS_NAME" > /dev/null; then echo "$PROCESS_NAME is running" else echo "$PROCESS_NAME is not running" fi
Description: Checks if a specific process is currently running.
7. Automate Git Commits
#!/bin/bash # Automate Git commit process git add . git commit -m "Automated commit on $(date)"
Description: Automatically stages and commits all changes in the current git repository.
8. Download Files Using wget
#!/bin/bash # Download a file from a URL URL="http://example.com/file.txt" wget "$URL"
Description: Downloads a file from the specified URL using wget
.
9. Email Notification for Disk Usage
#!/bin/bash # Notify when disk usage exceeds 90% THRESHOLD=90 USAGE=$(df / | awk 'NR==2 {print $5}' | sed 's/%//') if [ "$USAGE" -gt "$THRESHOLD" ]; then echo "Disk usage is at ${USAGE}%!" | mail -s "Disk Alert" user@example.com fi
Description: Sends an email alert if disk usage exceeds 90%.
10. Rename Multiple Files
#!/bin/bash # Rename files by appending a suffix for file in *.txt; do mv "$file" "${file%.txt}_backup.txt" done
Description: Renames all .txt
files in the current directory by adding a _backup
suffix.
11. File Permissions Change
#!/bin/bash # Change permissions of all files in a directory DIR="/path/to/directory" chmod -R 755 "$DIR"
Description: Changes the permissions of all files in the specified directory to 755
.
12. System Update Automation
#!/bin/bash # Automate system update for Ubuntu/Debian sudo apt update && sudo apt upgrade -y
Description: Updates the package lists and upgrades installed packages on a Debian-based system.
13. Check Internet Connection
#!/bin/bash # Check if connected to the internet if ping -c 1 google.com &> /dev/null; then echo "Internet is up" else echo "Internet is down" fi
Description: Checks for an active internet connection by pinging Google.
14. Generate SSH Keys
#!/bin/bash # Generate SSH keys for secure access ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Description: Generates a new SSH key pair for secure access.
15. Compress Files
#!/bin/bash # Compress files into a tar.gz archive tar -czf archive.tar.gz /path/to/directory
Description: Compresses the specified directory into a .tar.gz
file.
16. Uncompress Files
#!/bin/bash # Uncompress a tar.gz archive tar -xzf archive.tar.gz
Description: Extracts files from a .tar.gz
archive.
17. Count Lines in Files
#!/bin/bash # Count lines in all text files in the current directory for file in *.txt; do echo "$file: $(wc -l < "$file") lines" done
Description: Counts and displays the number of lines in each .txt
file in the current directory.
18. Fetch Current Weather
#!/bin/bash # Fetch current weather from a weather API API_KEY="your_api_key" CITY="your_city" curl -s "http://api.openweathermap.org/data/2.5/weather?q=$CITY&appid=$API_KEY&units=metric" | jq '.weather[].description'
Description: Retrieves and displays the current weather for a specified city using OpenWeatherMap API.
19. Backup MySQL Database
#!/bin/bash # Backup MySQL database DB_NAME="database_name" USER="username" PASSWORD="password" mysqldump -u "$USER" -p"$PASSWORD" "$DB_NAME" > "$DB_NAME"_backup.sql
Description: Backs up a specified MySQL database to a .sql
file.
20. Restore MySQL Database
#!/bin/bash # Restore MySQL database from a backup DB_NAME="database_name" USER="username" PASSWORD="password" mysql -u "$USER" -p"$PASSWORD" "$DB_NAME" < "$DB_NAME"_backup.sql
Description: Restores a MySQL database from a backup file.
21. Kill a Process
#!/bin/bash # Kill a specified process by name PROCESS_NAME="my_process" pkill "$PROCESS_NAME"
Description: Terminates a process by its name.
22. Send a Test Email
#!/bin/bash # Send a test email using mail command echo "Test email body" | mail -s "Test Subject" user@example.com
Description: Sends a test email to a specified address.
23. Monitor Log File for Changes
#!/bin/bash # Monitor a log file for changes tail -f /path/to/logfile.log
Description: Continuously monitors and displays changes to a specified log file.
24. Create a New User
#!/bin/bash # Create a new user with a specified username USERNAME="newuser" sudo adduser "$USERNAME"
Description: Creates a new user account with the specified username.
25. Change User Password
#!/bin/bash # Change password for a specified user USERNAME="existinguser" echo "Enter new password for $USERNAME:" passwd "$USERNAME"
Description: Prompts to change the password for a specified user.
26. View System Uptime
#!/bin/bash # Display system uptime uptime
Description: Displays how long the system has been running.
27. Check Running Services
#!/bin/bash # List active services systemctl list-units --type=service --state=running
Description: Lists all active services on the system.
28. Flush DNS Cache
#!/bin/bash # Flush DNS cache sudo systemd-resolve --flush-caches
Description: Clears the DNS cache on a system.
29. Create a Virtual Environment (Python)
#!/bin/bash # Create a Python virtual environment ENV_NAME="myenv" python3 -m venv "$ENV_NAME"
Description: Creates a new Python virtual environment.
30. Activate a Virtual Environment
#!/bin/bash # Activate a Python virtual environment source myenv/bin/activate
Description: Activates the specified Python virtual environment.
31. Deactivate a Virtual Environment
#!/bin/bash # Deactivate a Python virtual environment deactivate
Description: Deactivates the current Python virtual environment.
32. Remove Empty Directories
#!/bin/bash # Remove all empty directories in the specified path find /path/to/directory -type d -empty -delete
Description: Deletes all empty directories within the specified path.
33. Search for a Pattern in Files
#!/bin/bash # Search for a pattern in all text files in the current directory PATTERN="search_term" grep -r "$PATTERN" *.txt
Description: Searches for a specified pattern in all .txt
files in the current directory.
34. Count Word Frequency
#!/bin/bash # Count the frequency of words in a file FILE="file.txt" tr -c '[:alnum:]' '[\n*]' < "$FILE" | tr '[:upper:]' '[:lower:]' | sort | uniq -c | sort -nr
Description: Counts and displays the frequency of each word in a specified file.
35. Extract Unique Lines from a File
#!/bin/bash # Extract unique lines from a file FILE="file.txt" sort -u "$FILE" -o "$FILE"
Description: Sorts a file and removes duplicate lines.
36. Monitor CPU Usage
#!/bin/bash # Monitor CPU usage top -b -n 1 | head -n 10
Description: Displays the top 10 processes by CPU usage.
37. Monitor Memory Usage
#!/bin/bash # Monitor memory usage free -h
Description: Displays memory usage in a human-readable format.
38. List Network Connections
#!/bin/bash # List current network connections netstat -tuln
Description: Lists all current TCP/UDP network connections.
39. Check System Hardware Information
#!/bin/bash # Display system hardware information lshw -short
Description: Provides a short summary of hardware information on the system.
40. Change Hostname
#!/bin/bash # Change the system hostname NEW_HOSTNAME="new_hostname" sudo hostnamectl set-hostname "$NEW_HOSTNAME"
Description: Changes the system hostname to the specified value.
41. Download Multiple Files
#!/bin/bash # Download multiple files from a list while read -r URL; do wget "$URL" done < urls.txt
Description: Downloads files from a list of URLs specified in urls.txt
.
42. Check Server Response Time
#!/bin/bash # Check server response time URL="http://example.com" curl -o /dev/null -s -w "%{time_total}\n" "$URL"
Description: Measures the response time for a specified URL.
43. Extract File Extensions
#!/bin/bash # Extract and display unique file extensions in the current directory ls | awk -F. '{if (NF>1) print $NF}' | sort -u
Description: Lists unique file extensions present in the current directory.
44. Update Package List (CentOS)
#!/bin/bash # Update package list for CentOS/RHEL sudo yum update -y
Description: Updates the package list for CentOS/RHEL systems.
45. Get Public IP Address
#!/bin/bash # Get the public IP address curl -s ifconfig.me
Description: Retrieves and displays the public IP address of the system.
46. Create a Zip Archive
#!/bin/bash # Create a zip archive of a directory zip -r archive.zip /path/to/directory
Description: Compresses the specified directory into a .zip
file.
47. Unzip a Zip Archive
#!/bin/bash # Unzip a zip archive unzip archive.zip
Description: Extracts files from a .zip
archive.
48. View Top 10 Memory-Consuming Processes
#!/bin/bash # Display top 10 memory-consuming processes ps aux --sort=-%mem | head -n 11
Description: Lists the top 10 processes consuming the most memory.
49. Find and Remove Duplicate Files
#!/bin/bash # Find and remove duplicate files based on MD5 hash find /path/to/directory -type f -exec md5sum {} + | sort | uniq -w32 -dD | awk '{print $2}' | xargs rm -f
Description: Finds and deletes duplicate files based on their MD5 hash.
50. Check System Temperature
#!/bin/bash # Check CPU temperature (requires lm-sensors) sensors | grep 'Core 0'
Description: Displays the temperature of the first CPU core.
51. Export MySQL Database
#!/bin/bash # Export MySQL database DB_NAME="database_name" USER="username" PASSWORD="password" mysqldump -u "$USER" -p"$PASSWORD" "$DB_NAME" > "$DB_NAME"_export.sql
Description: Exports a MySQL database to a .sql
file.
52. Import MySQL Database
#!/bin/bash # Import MySQL database DB_NAME="database_name" USER="username" PASSWORD="password" mysql -u "$USER" -p"$PASSWORD" "$DB_NAME" < "$DB_NAME"_export.sql
Description: Imports a MySQL database from a .sql
file.
53. Create a Log File
#!/bin/bash # Create a log file with the current date and time LOG_FILE="log_$(date +%Y%m%d).log" echo "Log created on $(date)" > "$LOG_FILE"
Description: Creates a new log file with a timestamp in its name.
54. Archive Old Logs
#!/bin/bash # Archive log files older than 7 days find /path/to/logs -type f -mtime +7 -exec gzip {} \;
Description: Compresses log files older than 7 days.
55. Get Process ID
#!/bin/bash # Get the process ID of a specified process PROCESS_NAME="my_process" PID=$(pgrep "$PROCESS_NAME") echo "Process ID of $PROCESS_NAME: $PID"
Description: Retrieves and displays the process ID of a specified process.
56. Check Available Disk Space
#!/bin/bash # Check available disk space df -h
Description: Displays available disk space for all mounted filesystems.
57. List All Users
#!/bin/bash # List all users on the system cut -d: -f1 /etc/passwd
Description: Lists all user accounts on the system.
58. Get System Information
#!/bin/bash # Get system information uname -a
Description: Displays detailed information about the system.
59. Clean Up Temporary Files
#!/bin/bash # Clean up temporary files rm -rf /tmp/*
Description: Deletes all files in the /tmp
directory.
60. Change File Ownership
#!/bin/bash # Change ownership of files in a directory CHOWN_DIR="/path/to/directory" sudo chown -R user:group "$CHOWN_DIR"
Description: Changes ownership of all files in a specified directory.
61. Set Environment Variables
#!/bin/bash # Set environment variables export VAR_NAME="value"
Description: Sets an environment variable for the session.
62. Display Current Date and Time
#!/bin/bash # Display current date and time date
Description: Outputs the current date and time.
63. Send HTTP Request
#!/bin/bash # Send a GET request to a specified URL curl -X GET "http://example.com/api"
Description: Sends an HTTP GET request to the specified API endpoint.
64. Check for Package Updates (Debian)
#!/bin/bash # Check for package updates on Debian-based systems sudo apt update
Description: Checks for available updates for installed packages.
65. Update All Packages (Debian)
#!/bin/bash # Update all installed packages on Debian-based systems sudo apt upgrade -y
Description: Upgrades all installed packages to their latest versions.
66. Clear the Terminal Screen
#!/bin/bash # Clear the terminal screen clear
Description: Clears the terminal screen.
67. Set Up Cron Job
#!/bin/bash # Set up a cron job for running a script every day at midnight (crontab -l ; echo "0 0 * * * /path/to/script.sh") | crontab -
Description: Adds a cron job to run a specified script daily at midnight.
68. Count Down Timer
#!/bin/bash # Simple countdown timer SECONDS=10 while [ $SECONDS -gt 0 ]; do echo "$SECONDS seconds remaining" sleep 1 ((SECONDS--)) done echo "Time's up!"
Description: Implements a countdown timer for a specified duration.
69. Display Disk Usage
#!/bin/bash # Display disk usage of the current directory du -sh .
Description: Shows the disk usage of the current directory in a human-readable format.
70. Show Last Login
#!/bin/bash # Show last login information of users last
Description: Displays the last login times of users.
71. List Open Files
#!/bin/bash # List all open files by a specified process PROCESS_NAME="my_process" lsof -c "$PROCESS_NAME"
Description: Lists all open files by a specified process.
72. Check Firewall Status
#!/bin/bash # Check the status of the firewall sudo ufw status
Description: Displays the current status of the firewall.
73. Check Services Status
#!/bin/bash # Check the status of a specific service SERVICE_NAME="service_name" systemctl status "$SERVICE_NAME"
Description: Checks and displays the status of a specified service.
74. Create a Simple Backup Script
#!/bin/bash # Simple backup script SOURCE="/path/to/source" DESTINATION="/path/to/destination" tar -czf "$DESTINATION/backup_$(date +%Y%m%d).tar.gz" "$SOURCE"
Description: Backs up a specified directory to a destination using tar.
75. Change File Permissions
#!/bin/bash # Change permissions of a file FILE="file.txt" chmod 644 "$FILE"
Description: Changes the permissions of a specified file.
76. Display Current User
#!/bin/bash # Display the current user whoami
Description: Outputs the name of the currently logged-in user.
77. Backup MySQL Database
#!/bin/bash # Backup MySQL database with date in filename DB_NAME="database_name" USER="username" PASSWORD="password" mysqldump -u "$USER" -p"$PASSWORD" "$DB_NAME" > "${DB_NAME}_$(date +%Y%m%d).sql"
Description: Backs up a MySQL database with the current date in the filename.
78. List Files by Modification Date
#!/bin/bash # List files in current directory sorted by modification date ls -lt
Description: Lists files in the current directory sorted by modification date, with the most recent first.
79. Check Disk I/O
#!/bin/bash # Check disk I/O statistics iostat
Description: Displays I/O statistics for devices and partitions.
80. Ping a Host
#!/bin/bash # Ping a specified host HOST="example.com" ping -c 4 "$HOST"
Description: Pings a specified host and displays the response times.
81. Search for a File
#!/bin/bash # Search for a file by name FILE_NAME="target_file.txt" find /path/to/search -name "$FILE_NAME"
Description: Searches for a specified file in a given directory and its subdirectories.
82. Check Running Services
#!/bin/bash # Check all running services systemctl list-units --type=service --state=running
Description: Lists all currently running services on the system.
83. Install a Package (Debian)
#!/bin/bash # Install a specified package on Debian-based systems PACKAGE_NAME="package_name" sudo apt install -y "$PACKAGE_NAME"
Description: Installs a specified package on Debian-based systems.
84. Create a New User
#!/bin/bash # Create a new user USERNAME="newuser" sudo adduser "$USERNAME"
Description: Creates a new user account on the system.
85. Delete a User
#!/bin/bash # Delete a specified user USERNAME="username_to_delete" sudo deluser "$USERNAME"
Description: Deletes a specified user account from the system.
86. View System Logs
#!/bin/bash # View system logs sudo less /var/log/syslog
Description: Opens the system log file for viewing.
87. Get Disk Usage Summary
#!/bin/bash # Get a summary of disk usage df -h --total
Description: Displays a summary of disk usage for all mounted filesystems.
88. List Installed Packages
#!/bin/bash # List all installed packages (Debian) dpkg --get-selections | grep -v deinstall
Description: Lists all installed packages on Debian-based systems.
89. Create SSH Key
#!/bin/bash # Create a new SSH key pair ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Description: Generates a new RSA SSH key pair for secure access.
90. List Active SSH Connections
#!/bin/bash # List active SSH connections ss -tna | grep ':22'
Description: Displays all active SSH connections on port 22.
91. Run a Command at Specific Time
#!/bin/bash # Run a command at a specified time echo "your_command" | at 10:30
Description: Schedules a command to run at a specific time using the at
command.
92. Generate a Random Password
#!/bin/bash # Generate a random password < /dev/urandom tr -dc A-Za-z0-9 | head -c 16 ; echo
Description: Generates a random 16-character alphanumeric password.
93. Download a File
#!/bin/bash # Download a file from a URL URL="http://example.com/file.txt" wget "$URL"
Description: Downloads a file from a specified URL.
94. Check for Broken Links
#!/bin/bash # Check for broken links in a specified HTML file FILE="index.html" lynx -dump -nonumbers -force_html "$FILE" | grep -Eo '(http|https)://[^"]+' | xargs -n1 curl -Is | grep "404"
Description: Checks for broken links in a specified HTML file.
95. Create a Simple Web Server
#!/bin/bash # Create a simple web server using Python python3 -m http.server
Description: Starts a simple web server using Python, serving files from the current directory.
96. Archive Files Older Than 30 Days
#!/bin/bash # Archive files older than 30 days find /path/to/files -type f -mtime +30 -exec tar -rvf archive.tar {} +
Description: Archives files older than 30 days into a .tar
file.
97. Get Weather Information
#!/bin/bash # Get weather information for a specified location LOCATION="London" curl "http://wttr.in/$LOCATION?format=3"
Description: Retrieves and displays weather information for a specified location.
98. Find Large Files
#!/bin/bash # Find files larger than 100MB find / -type f -size +100M
Description: Searches for and lists files larger than 100 MB on the system.
99. Check System Load Average
#!/bin/bash # Check the system load average uptime
Description: Displays the system load average for the past 1, 5, and 15 minutes.
100. Get User Information
#!/bin/bash # Get information about the current user id
Description: Displays user information for the currently logged-in user.
These shell scripts can be directly utilized or slightly modified to meet specific needs in various scenarios. If you need further assistance with any particular script or concept, feel free to ask!
Top comments (0)