Shell Script to Automate System Updates and Upgrades in Linux
Last Updated :
21 Aug, 2023
Shell Scripts are often used for automating repetitive tasks that we can perform on a daily basis. With shell scripts, you can automate tasks like renaming a bunch of files or making backup scripts and many more complex tasks. As we all know, we often need to update and upgrade Linux packages and delete the older version and unnecessary files and get rid of them for better performance, so we execute a bunch of commands like update, upgrade, cache clear commands, etc. We are going to automate this task of updating and upgrading using shell script in a very simple way.
What is a Linux Distro?
We all have heard a lot about Linux distribution, which is a variant of the Linux operating system that includes the Linux kernel. Linux is a collection of software tools, and applications and comes with GUI. There are n numbers of Linux distributions, some of which include Ubuntu, Arch Linux, Fedora, and many others. These operating systems are often popular in pentesting and ethical hacking.
Is it necessary to update/upgrade your package list?
Well, when it comes to updating/upgrading it is considered a good practice if you work on one of the Linux distros. When you update/upgrade the packages, you are refreshing the available list of the packages and their versions, which are often updated to fix any security vulnerabilities. It's generally recommended to update your packages at least once a week.
Update and upgrade your package list using Bash Script
Step 1: Open a text editor
If we are going to work on a bash script, we will need a text editor such as nano or vim, open the terminal and type the following command:
nano update.sh
Above command will open a new file called update.sh in the nano editor.
Step 2: Writing the script
The script is very simple. It only contains a bunch of commands that are going to be executed one after and perform update and upgrade. let's see the script below:
#!/bin/bash
# below command will Update package lists
sudo apt update
# below command will Upgrade the packages that can be upgraded
sudo apt upgrade -y
# below command will Remove unnecessary packages and dependencies for good memory management
sudo apt autoremove -y
# below command will Clean package cache
sudo apt clean -y
# below command will Display system update status on terminal to know if the update and upgrade is successfull
echo "System updates and upgrades completed successfully."
The above script is very simple and easy to understand, and it can save you the time and effort of writing update commands over and over. After writing the script, save and close the file by pressing Ctrl + O followed by Enter, then Ctrl + X.
Step 3: Make the script executable.
Before we run the script, we also need to make it executable so that it can run without any issues. to do so type in the following command:
chmod +x update.sh
Step 4: Run the script
Now everything is done and we can test our script by running it with the following command:
./update.sh
This will run the update.sh script and update and upgrade your system.
How to fix broken packages
Step 1: We can always check for broken packages if there are any by the following command:
sudo apt-get check
Step 2: After we find packages that are troubling or broken, we can just remove them by using the following command:
sudo apt-get remove package-name
Step 3: If you have tried installing or updating a package and it still does not work, you can try using the force method in order to resolve the issue by the following command:
sudo apt-get install -f

Conclusion
In conclusion, using a shell script to automate system updates and upgrades in Linux is a simple and effective way to keep your system up to date. You can alYouexplore more and make things more interesting by adding more functionalities and more effective ways to make things happen. You can follow the above steps to automate the task of updating and upgrading. You can also add more to the script like checking for root access to run the script, checking for internet connectivity before running the script, and many more.
Similar Reads
Introduction to Linux Shell and Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
8 min read
How to Create a Shell Script in linux
Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
Linux Shell Script to Backup Files and Directory
Backing up our important files and directories is crucial to safeguarding our data. In this article, weâll create a simple shell script that uses the tar utility to create an archive file for backup purposes that we can use whenever we require. The shell script weâll build allows us to specify which
5 min read
How to Run a Shell Script in Linux
Shell scripts are a powerful way to automate tasks and manage system processes in Linux. These scripts, written in shell programming languages like Bash, allow users to execute a sequence of commands efficiently. In this guide, we'll show the steps to check a shell script in Linux before running it,
4 min read
A Versatile System Backup Script for Linux
System Tar and Restore is a flexible system and it has two bash scripts one is the main script star.sh and the other is the GUI wrapper star-gui.sh. It has three modes accessible in the System Tar and Restores system namely: Backup, Restore, and Transfer.Backup: With this, we can take the tar backup
4 min read
Lynis - Security Tool for Audit and Hardening Linux Systems
Lynis is an open-source security auditing tool for UNIX derivatives like Linux, Mac OS, BSD, other Unix-based operating systems etc. Performing extensive health scan of systems that support System Hardening and Compliance Testing. An open-source software with GPL License. This tool also scans for ge
11 min read
Linux Shell Script to Sync Directories Interactively
Scripts that sync directories are scripts used to synchronize the contents of two directories. This means that the script will ensure that the two directories have duplicate files and directories and that the contents of the files are the same. There are various ways to sync directories, and the spe
7 min read
Getting System and Process Information Using C Programming and Shell in Linux
Whenever you start a new process in Linux it creates a file in /proc/ folder with the same name as that of the process id of the process. In that folder, there is a file named "status" which has all the details of the process. We can get those Process Information Through shell as follows: cat /proc/
2 min read
How to Schedule Tasks Using at Command in Linux
The at command in Linux is used to schedule one-time tasks to be executed at a specified time in the future. It allows users to submit a command or script for execution at a later time, offering a convenient way to automate tasks without the need for complex cron jobs. The scheduled jobs are managed
5 min read
How to Start, Stop and Restart Services in Linux Using systemctl Command
System services play a crucial role in the functioning of a Linux system, handling various tasks and processes in the background. systemctl is a powerful command-line tool that allows users to manage these services effectively. In this article, we will explore the basics of using systemctl to start,
9 min read