Shell Scripting: Automate Linux Updates, Upgrades and Clean-up Operations

Shell Scripting: Automate Linux Updates, Upgrades and Clean-up Operations

This article will teach you how to take care of your Linux Operating System (OS). You will learn how to efficiently update, upgrade, clean, and fix broken installs using shell commands. In the end you will also get a bonus shell script called janitor to automate the whole process for you!

This article is intended for Unix users. The methods and tricks shared here work for any Linux distribution (Ubuntu was used for demonstration).

The main key points are:

  • What is a Linux distro
  • Why and how should you update/upgrade your package list
  • How to fix broken packages/dependencies
  • How to clean junk files
  • What is the janitor process
  • How to automate the janitor process using unix-janitor shell script

What is a Linux distro?

Just like Windows, iOS, and MacOS, Linux is an OS.

A Linux distribution -- often shortened to "Linux distro" -- is a version of the open-source Linux operating system that is packaged with other components, such as installation programs, management tools and additional software such as the KVM hypervisor.

Linux distributions, which are based on the Linux kernel, are often easier for users to deploy than the traditional open-source version of Linux. This is because most distributions eliminate the need for users to manually compile a complete Linux operating system from source code and because they are often supported by a specific vendor.

Many programmers prefer to use Linux over other operating systems available. There are so many distros available. Whichever you have, it is important that you take care of it for its proper functioning.

Is it necessary to update/upgrade your package list?

Linux Kernel is an important piece of program that forms the first layer of interaction between your computer’s hardware and your OS. Its job is to act as a mediator between your Linux OS and the hardware of your computer like the graphics card, RAM, CPU, so on and so forth.

Like any other software, Linux Kernel also needs an update periodically. Linus Torvalds releases the new updates to the Linux Kernels. Every update typically includes fixes to security loopholes, bug fixes to problems, better hardware compatibility, improved stability, more speed, and occasionally major updates also bring some new functions and features. Hence you will see several Linux Kernels all over the internet. This is a completely independent release and no Linux distribution control it.

If you have a modern PC and you are on the internet most of the time, and security fixes are of utmost importance to you, then you should upgrade to the latest kernel. It may make your PC faster, safer, and have better compatibility with your Linux OS.

It is essential to update installed packages before upgrading them because the system cannot know whether the repository has a new version of a package unless it has an up-to-date copy of the package list.

How to update and upgrade your package list

In the terminal type, the following command

sudo apt-get update -y && sudo apt-get upgrade -y

apt-get update updates the list of available packages and their versions, but it does not install or upgrade any packages. apt-get upgrade install newer versions of the packages you have. After updating the lists, the package manager knows about available updates for the software you have installed.

The -y flag is a --yes flag simply answers yes to any questions that come up so the install won't hang waiting for someone to press a key. The && is used to chain the two commands together.

How to fix broken packages

Package managers in Linux allow you to control the installation and removal of packages. In addition to that, package managers also help you in finding broken packages on your system and reinstalling them to fix various issues associated with Linux packages.

When you install a new package in Linux, your system's package manager is in charge of the whole installation process. These package managers have built-in methods to handle exceptions and errors. But sometimes, in case of unexpected issues, the installation halts and the complete package isn't installed. Such packages are called broken packages in Linux.

Package managers like APT do not allow the further installation of packages if it finds a broken package on the system. In such a situation, repairing the broken package is the only choice to go for.

Every package manager handles different types of packages. For example, DNF and YUM (Yellow-Dog Updater Modified) work with the Red-Hat Package Manager (RPM) to download and install RPM packages. Similarly, APT acts as a frontend wrapper for the base dpkg software on Debian-based distributions. We are going to use apt in our example. Fix broken packages:

sudo apt --fix-missing update -y

Update your system's package list from the available sources:

sudo apt update -y

Force the installation of the broken packages using the -f flag. APT will automatically search for broken packages on your system and reinstall them from the official repository.

sudo apt install -f -y

Clean all the JUNK

Unlike Windows, which comes with built-in defrag and disk clean-up tools, Ubuntu doesn’t make it immediately obvious how you go about trying to free up space.

By default Ubuntu keeps every update it downloads and installs in a cache on your disk, just in case you need it again. This is useful if you regularly add and remove apps, find yourself needing to reconfigure/reinstall a specific package, or simply have a poor connection.

The apt-get clean command helps to clean out the cache once you have installed the packages using apt-get install command in your system.

sudo apt-get clean -y

The apt-get autoclean option, like apt-get clean, clears the local repository of retrieved package files, but it only removes files that can no longer be downloaded and are virtually useless. It helps to keep your cache from growing too large.

sudo apt-get autoclean -y

The autoremove option removes packages that were automatically installed because some other package required them but, with those other packages removed, they are no longer needed. Sometimes, an upgrade will suggest that you run this command.

sudo apt-get autoremove -y

After running the steps manually, it is recommended to always update/upgrade your packages at last.

All about the JANITOR process

A janitor is one who keeps the premises of a building (such as an apartment or office) clean, tends the premises' system, and makes minor repairs. How does a janitor relate to the content of this article? Well, I wrote a simple shell script to automate all the cleaning, updating, and taking care of your system.

Instead of manually typing one command at a time and wait for its execution, you can now run the script once and all the janitor process will be done automatically. Depending on the success of the execution of the commands, you may have to run the script more than once.

More information on janitor.

Was this article helpful? Let me know in the comments below.