Remove a terminal command

Is it possible to remove a terminal command? For example, if there was a command call lookup (not an alias), how would I go about removing it?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

A better way would be to redirect the output of the command to /dev/null. That way, the command will ‘be there’ but will show no results.

Example: the ls command.

  1. Make an alias
    alias ls = "ls > /dev/null"
  2. Running ls now will not show any results.

EDIT: Thanks to @scai
In the answer above, the command will still show errors because we have redirected stdout but not stderr plus a more serious issue, it will still run the command.

Quoting @scai:

If the command in question does things you don’t like, for example creating or deleting files, changing settings, painting your nose blue, then this alias won’t stop it from doing these things.

For the workaround, we will just alias the command to an empty string, then just redirect the resulting error to /dev/null.

alias ls = "" > /dev/null 2>&1

That’s much better now.


EDIT 2: Thanks to @scai again 🙂
This is a temporary solution.
To revert back to the original function of the command (in our case ls), just run unalias ls.

Solution 2

You can do that but you shouldn’t.

Anyway there are four main places for commands.

/bin & /usr/bin & /sbin & /usr/sbin

To know the command you want where is

whereis COMMAND

Then as a root you can delete or just rename or move to other directory

sudo rm /path/command

Or

sudo mv /path/commad /newPath

Be sure that the newPath is not in your $PATH environment

Solution 3

I think this “proper” way to do this would be to use permissions. Let’s take the ls command as an example as well.

-rwxr-xr-x 1 root root 105840 /bin/ls*

Here’s how I would do it :

Create a group for users allowed to use ls, let’s say we call it lsusers.

sudo addgroup lsusers

Add users to this group accordingly

sudo usermod -aG lsusers [username]

Remove world permissions on /bin/ls

sudo chmod o= /bin/ls

Change the group ownership of /bin/ls

sudo chgrp lsusers /bin/ls

Make sure group permission allow at least executing

sudo chmod 0710 /bin/ls

This should bring something like :

-rwx--x--- 1 root lsusers 105840 /bin/ls*

Which allows root and lsusers members to execute /bin/ls. Actually, the big problem of aliases is that they can be overridden most of the time. Changing permissions of something belonging to root, now that is a little harder.

Now, about real removal, well this is a package problem. If you want to remove a program (command) from your system, just ask for its package to be removed with :

sudo apt-get --purge remove [package name]

Of course, this is not the kind of things you could do with /bin/ls (and I’m not sure you’d like to actually rm it :p)

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply