CLI for Efficiency #1

sensen

sensen

Posted on November 19, 2019

CLI for Efficiency #1

I tried KDE Plasma on my Ubuntu machine last weekend. After using it for a while, I decided that it is too complicated for me and I'd like to remove them from my machine.

To remove installed packages in Ubuntu is simple.

$ sudo apt remove package-name-1 package-name-2

But, there are a lot of packages installed automatically when you install KDE Plasma. Removing them one by one is definitely, well, inefficient. So, the challenge here is to get a list of installed KDE packages in proper format, so you can feed them into the command above.

Here's how I solve it in an efficient way.

apt as the interface for the package management system in Ubuntu provides a way for you to list installed packages. Here's one way to list installed packages which name contains kde.

$ apt list --installed | grep kde

.
.
debconf-kde-helper/disco 1.0.3-1 amd64 [installed,automatic]
kde-style-breeze/disco 4:5.15.4.1-0ubuntu1 [installed,automatic]
.
.

The output is not something we can feed into apt remove command directly, so we need to sanitize it quite a bit.

It's obvious that the package name is anything before /. We can use the cut command to do just that.

$ apt list --installed | grep kde | cut -d "/" -f 1

.
.
debconf-kde-helper
kde-style-breeze
.
.

Next, since each packages are listed in a new line, we still can't pass this output directly to the apt remove command. Let's replace the new line with space using tr command.

$ apt list --installed | grep kde | cut -d "/" -f 1 | tr '\n' ' '

.
.
debconf-kde-helper kde-style-breeze
.
.

Now, the output of installed KDE packages are now in proper format. We can now feed the output directly to apt remove command using xargs.

$ apt list --installed | grep kde | cut -d "/" -f 1 | tr '\n' ' ' | xargs sudo apt remove

It's amazing how these four simple tools: grep, cut, tr, and xargs, can help you automate and achieve your goal; saving you from the manual and repetitive labor.

How might you do it differently?

💖 💪 🙅 🚩
sensen
sensen

Posted on November 19, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related