Open In App

Shell Script to Automate System Updates and Upgrades in Linux

Last Updated : 21 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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
creating bash file

 

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."
Bash Script for update and upgrade

 

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
making Script executable

 

Step 4: Run the script

Now everything is done and we can test our script by running it with the following command:

./update.sh

Running Script

 

Script ran successfully

 

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
checking for broken packages

 

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
removing a particular package

 

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

Installing a particular package with force option

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads