1. Introduction

As system administrators, it’s essential to update our Linux system often to maintain security, optimize performance, and gain access to new features. But even for seasoned users, switching between various commands and sequencing might be stressful. Building a single command can help us to perform the updates, improve accuracy, and save time.

In this tutorial, we’ll discuss the different ways of building a single command to perform operating system updates in a Linux environment.

We tested the commands and code on Ubuntu 20.04 LTS with Bash 5.0.17, and they should work well in most Debian environments.

2. Using the && (and) Operator

In Linux Shell, the && operator is a logical AND operator. The instruction to the right of && will be carried out if the command to the left of it is successful (exits with a status of 0, signifying success). However, the command on the right side will not run if the command on the left fails (exits with a non-zero status, indicating an error).

Also, this command uses the && (AND) operator to run a sequence of Linux commands one after the other:

$ sudo apt-get update -y && sudo apt-get full-upgrade -y && sudo apt-get dist-upgrade -y && sudo apt-get autoremove -y && sudo apt-get clean -y && sudo apt-get autoclean -y
[sudo] password for labenv:
Hit:1 http://us.archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
...
... output truncated ...
...
Building dependency tree... Done
Reading state information... Done

Here’s a quick rundown of each apt-get command option and its significance in the context of system updates:

  • update: Updates the package lists for available software packages
  • full-upgrade: Performs the entire system upgrade, upgrading available packages to their latest versions
  • dist-upgrade: Similar to full-upgrade but may handle dependencies differently, especially during distribution upgrades
  • autoremove: Automatically removes the available, but no longer required packages
  • clean: removes the package files that were obtained from the local repository
  • autoclean: Removes any partially downloaded package files from the local repository

Be sure to use the && operator to concatenate these commands, where each subsequent command is executed only upon the preceding command’s successful completion. This helps in automating system maintenance tasks and ensuring a smooth update process. We use the -y flag with each command to automatically answer yes to prompts, making the process non-interactive.

3. Using alias Command

Further, for concatenating the apt-get commands using the && operator, we can use the alias command to simplify the longer and more complex commands using custom names or abbreviations. In our case, we’re using system_upgrade as an alias for the sequence of apt-get commands concatenated using the && operator:

$ alias system_upgrade="sudo apt-get update -y && sudo apt-get full-upgrade -y && sudo apt-get dist-upgrade -y && sudo apt-get autoremove -y && sudo apt-get clean -y && sudo apt-get autoclean -y"

Now, we can simply call the system_upgrade command to execute all sequences of apt-get commands for system upgrades:

$ system_upgrade
Hit:1 http://us.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://security.ubuntu.com/ubuntu jammy-security InRelease
...
... output truncated ...
...
Building dependency tree... Done
Reading state information... Done

Also, these are inline aliases valid for the duration of the current terminal session. Once the terminal is closed, the alias gets cleared off. In the next section, we’ll see how to add the alias to the .bashrc file.

3.1. Adding in .bashrc File

An alias becomes permanent when defined in the .bashrc file. The .bashrc file is a script that runs whenever we start a new interactive shell session.

So, we can make sure that the alias is available each time we launch a new terminal window or session by adding the alias to our .bashrc file:

alias system_upgrade="sudo apt-get update -y && sudo apt-get full-upgrade -y && sudo apt-get dist-upgrade -y && sudo apt-get autoremove -y && sudo apt-get clean -y && sudo apt-get autoclean -y" >> ~/.bashrc

Now, we’ve appended the system_upgrade alias to the .bashrc file for the specific user space.

3.2. Adding to /etc/profile.d

Alternatively, we can keep these commands on the /etc/profile.d/ to execute during system startup. Also, these contents might include commands to configure environment variables or set system-wide settings. The changes made by these scripts apply to all user environments when they login to the terminal.

4. Using Bash Script

There are numerous uses for Bash scripting, including file management, job sequencing, automation, and more. Let’s now utilize Bash scripting to automate the complete system update by creating a simple script with our sequence of apt-get commands:

$ cat system_update.sh
#!/usr/bin/bash

set -e
sudo apt-get update -y
sudo apt-get full-upgrade -y
sudo apt-get dist-upgrade -y
sudo apt-get autoremove -y
sudo apt-get clean -y
sudo apt-get autoclean -y

Note that our script starts with a shebang line (#!/usr/bin/bash), indicating the path to run the script through the Bash interpreter. Subsequently, the set -e command facilitates a rapid script exit if any command it executes returns a non-zero status, signifying an error.

Further, let’s provide the execute permission to the Bash script for direct invocation:

$ chmod +x system_update.sh
$ ls -l system_update.sh
-rwxrwxr-x 1 labenv labenv 175 Jan  1 14:05 system_update.sh

As we can see, the execute permission is provided to the system_update.sh script using the chmod command.
Now, let’s attempt to execute the script using ./system_update.sh and initiate the system updates:

$ ./system_update.sh
[sudo] password for labenv:
Hit:1 http://us.archive.ubuntu.com/ubuntu jammy InRelease
The following package was automatically installed and is no longer required:
  libxmlsec1-nss
...
... output truncated ...
...
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done

This way, completing the system updates becomes easy by running a single Bash script.

5. Conclusion

In this article, we explored various methods of streamlining updates using apt-get commands with && operators, employing the alias command, and utilizing Bash scripts.

Consolidating these processes into a single command not only saves time but also promotes consistent maintenance practices for keeping our Linux environment secure and up-to-date.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.