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
Linux Commands Cheat Sheet
Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
grep command in Unix/Linux
The grep command in Unix/Linux is a powerful tool used for searching and manipulating text patterns within files. Its name is derived from the ed (editor) command g/re/p (globally search for a regular expression and print matching lines), which reflects its core functionality. grep is widely used by
7 min read
Sed Command in Linux/Unix With Examples
The SED command is one of the most powerful commands used during the process of text processing in Linux/Unix operating systems. The SED command is typically invoked for executing operations such as replace and search, text manipulation, and stream editing.With SED, you can manipulate text files wit
9 min read
25 Basic Linux Commands For Beginners [2025]
While performing a task, we all need shortcuts. Shortcuts help us to complete a task quickly. Linux comes with such commands which are one to two words, using that commands, you can perform several operations in no time. As a beginner, you must be aware of those basic Linux commands to complete an o
13 min read
AWK command in Unix/Linux with examples
Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. Awk is a utility that enables a programmer to write tiny but eff
8 min read
Linux/Unix Tutorial
Linux is a widely-used open-source operating system, similar to Windows, Mac, and Android. It shares similarities with Unix, another operating system known for its commercial use. Unix and Linux have comparable components, including the kernel, shell, and programs. Many commands in Unix and Linux ex
12 min read
How to Find a File in Linux | Find Command
Linux, renowned for its robust command-line interface, provides a suite of powerful tools for efficient file and directory management. Among these, the "find" command stands out as an indispensable asset, offering unparalleled versatility in searching for files based on diverse criteria. This articl
10 min read
ZIP command in Linux with examples
In Linux, the zip command compresses one or more files or directories into a single.zip archive file. This saves disk space, keeps data organized, and makes it simple to share or backup files. It's among the most used compression utilities, particularly when sharing large files via email or storing
6 min read
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 Securely Copy Files in Linux | scp Command
Secure file transfer is a crucial part of Linux systems administration. Whether moving sensitive files between local machines or transferring data between servers, SCP (Secure Copy Protocol) provides a fast, secure, and efficient way to copy files over a network. By utilizing SSH (Secure Shell), SCP
11 min read